【发布时间】:2011-05-17 06:46:15
【问题描述】:
List<MyClass> MyClassPro
{
get;set;
}
MyClass obj = new MyClass();
obj.MyClassPro = null;
考虑 MyClassPro 为空。在反射的情况下,我不会知道类名或属性名。
如果我尝试使用 GetType 来获取属性的类型,
Type t = obj.GetType();
它返回“System.Collections.Generic.list。但我的期望是将类型作为 MyClass。
我也尝试过类似的方式
foreach(PropertyInfo propertyInfo in obj.GetProperties())
{
if(propertyInfo.IsGenericType)
{
Type t = propertyInfo.GetValue(obj,null).GetType().GetGenericArguments().First();
}
}
但它返回错误,因为集合属性的值为 null,所以我们无法获取类型。
在这种情况下,我如何获取集合属性的类型。
请帮帮我!
提前致谢。
【问题讨论】:
-
您能否澄清一下,它看起来不会编译。
-
它不是一个完整的代码。假设情况,请给我答案。
标签: c# reflection