I am struggling on how to do this: I am trying to have the DoSomething method accept all classes which are derived from GenericClass < T > without having to dismantle the GenericDerived class to < GenericDerived, Base >.
class Program
{
static void Main(string[] args)
{
SomeClass.DoSomething<GenericDerived>(new GenericDerived()); //Still not compiler friendly
}
}
class Base { }
class GenericClass<T> where T : Base { }
class GenericDerived : GenericClass<Base> { }
class SomeClass
{
public static void DoSomething<M, B>(M instance)
where B : Base
where M : GenericClass<B>
{
// Do something...
}
}
You don't have a type parameter named T in your example. You need to add it and its constraints too.
class SomeClass
{
public void DoSomething<M,T>(M instance) where T : Base
where M : GenericClass<T>
{
// Do something...
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.