Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

can i cast to a generic type with base Classes?

$
0
0

Hi i have this

pulic class BackGroundClass<T,X> : BaseClass where T : AnotherBaseClass
                                             where X : AnotherBaseClass
{
}

public class MyClassA : BackGroundClass<FooA, BarA>// FooA and BarA both inherit from AnotherBaseClass<T,X>
{
}

public class MyClassB : BackGroundClass<FooB, BarB>// FooB and BarB both inherit from AnotherBaseClass<T,X>
{
}

public class MyClassC : BackGroundClass<FooC, BarC>// FooC and BarC both inherit from AnotherBaseClass<T,X>
{
}

now what i want to is is have one list which contains multiple instances of MyClassA, MyClassB and MyClassC

i would be happy with

var myList = new List<BackGroundClass<AnotherBaseClass, AnotherBaseClass>>()

but if I do

var myClassC = new MyClassC ();

myList.Add(myClassC);

it doesn't work and myList is null

doing

someMethod( myClassC as BackGroundClass<AnotherBaseClass, AnotherBaseClass>)

also doesn't work

any suggestions would be appriciated.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>