Hot to get generic type from object type
- by Murat
My Classes are;
class BaseClass
{
}
class DerivedClass1 : BaseClass
{
}
class GenericClass<T>
{
}
class DerivedClass2 : BaseClass
{
GenericClass<DerivedClass1> subItem;
}
I want to access all fields of DerivedClass2 class.
I use System.Reflection and FieldInfo.GetValue() method;
Bu I cant get subItem field.
FieldInfo.GetValue() method return type is "object".
And I cant cast to GenericClass<DerivedClass1>
or
I cant get DerivedClass1 type.
I try this with BaseClass
BaseClass instance = FieldInfo.Getvalue(this) as GenericClass<BaseClass>;
but instance is null.
How to get instance with type or how to get only type?