lundi 6 février 2017

Generic type from base interface

My class structure (simplified)

interface Foo<T> { }
abstract class Bar1 : Foo<SomeClass> { }
abstract class Bar2 : Foo<SomeOtherClass> { }
class FinalClass1 : Bar1 { }
class FinalClass2 : Bar2 { }

Now, having only the type FinalClass1 and FinalClass2 I need to get their respective T types from the Foo interface - SomeClass for FinalClass1 and SomeOtherClass for FinalClass2. The abstract classes can implement more generic interfaces, but always only one Foo.

  1. How can I achieve this, using reflection?

  2. How can I also ensure that the type is implementing Foo regardless of what type the T is? Something like

bool bIsFoo = typeof(SomeType).IsAssignableFrom(Foo<>)

The above doesn't work.





Aucun commentaire:

Enregistrer un commentaire