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.
-
How can I achieve this, using reflection?
-
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