如何通过反射区分值类型、可空值类型、枚举、可空枚举和引用类型?
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中,这些信息可以被检索到哪里?
int
是一种值类型,int?
也是一种值类型。大多数基元类型都是值类型。 @Saqib 不确定您想通过此链接告诉我们什么。它没有包含您所谓的“基本类型”的定义。它将DateTime
称为基元,这是错误的。而且我从未听说过FtnPtr
类型,在内置引用类型下列出了它们。 - CodesInChaos