Hot to get generic type from object type

Posted by Murat on Stack Overflow See other posts from Stack Overflow or by Murat
Published on 2011-03-06T16:06:44Z Indexed on 2011/03/06 16:10 UTC
Read the original article Hit count: 302

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics