【发布时间】:2011-12-23 09:59:41
【问题描述】:
如何通过反射区分value-type、nullable value-type、enum、nullable-enum、reference-types?
enum MyEnum
{
One,
Two,
Three
}
class MyClass
{
public int IntegerProp { get; set; }
public int? NullableIntegerProp { get; set; }
public MyEnum EnumProp { get; set; }
public MyEnum? NullableEnumProp { get; set; }
public MyClass ReferenceProp { get; set; }
}
class Program
{
static void Main(string[] args)
{
Type classType = typeof(MyClass);
PropertyInfo propInfoOne = classType.GetProperty("IntegerProp");
PropertyInfo propInfoTwo = classType.GetProperty("NullableIntegerProp");
PropertyInfo propInfoThree = classType.GetProperty("EnumProp");
PropertyInfo propInfoFour = classType.GetProperty("NullableEnumProp");
PropertyInfo propInfoFive = classType.GetProperty("ReferenceProp");
propInfoOne.???
...............
...............
}
}
可以在propInfo...s 中的什么位置检索这些信息?
【问题讨论】:
-
如何定义“基本类型”?
-
int、float、double........正如你在 MyClass-props 中看到的那样。值类型。
-
那么自定义结构呢?例如,我不明白为什么要将枚举与其他值类型区分开来。
-
以上代码中没有值类型,值类型是一个结构体..这和原始类型不同。
标签: c# reflection propertyinfo