List<MyClass> MyClassPro
{
get;set;
}
MyClass obj = new MyClass();
obj.MyClassPro = null;
Consider the MyClassPro is null. In situation of Reflection i wont be knowing the Classname or Property Name.
If i try to get the Type of property using GetType like ,
Type t = obj.GetType();
It is returning “System.Collections.Generic.list. But my expectation is to get the Type as MyClass.
I also tried the way like
foreach(PropertyInfo propertyInfo in obj.GetProperties())
{
if(propertyInfo.IsGenericType)
{
Type t = propertyInfo.GetValue(obj,null).GetType().GetGenericArguments().First();
}
}
But it is returning error because of the Value of the collection property is null so we cant get the Type.
In this situation how can i get the Type of a collection Property.
Please help me !
Thanks in Advance.
Use
propertyInfo.PropertyTypeinstead ofpropertyInfo.GetValue(obj,null).GetType()which should give you the property type even if the property value isnull.So when you have a class like
and an instance of
Fooinobj, thenwill give you the value
System.String(as aSystem.Typeinstance) intypeArg.