如何价值型,空值类型,枚举,可为空,枚举,引用类型通过反射区分? [英] How to differentiate between value-type, nullable value-type, enum, nullable-enum, reference-types through reflection?

查看:515
本文介绍了如何价值型,空值类型,枚举,可为空,枚举,引用类型通过反射区分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何价值型,可为空值类型,枚举,可为空,枚举区分,引用类型通过反射?



 枚举MyEnum 
{
一,
两组,

}

类MyClass的
{
公众诠释IntegerProp {搞定;组; }
公众诠释? NullableIntegerProp {搞定;组; }
公共MyEnum EnumProp {搞定;组; }
公共MyEnum? NullableEnumProp {搞定;组; }
公共MyClass的ReferenceProp {搞定;组; }
}

类节目
{
静态无效的主要(字串[] args)
{
串propOne =IntegerProp;
串propTwo =NullableIntegerProp;
串propThree =EnumProp;
串propFour =NullableEnumProp;
串propFive =ReferenceProp;

型classType所= typeof运算(MyClass的);

的PropertyInfo propInfoOne = classType.GetProperty(propOne);
的PropertyInfo propInfoTwo = classType.GetProperty(propTwo);
的PropertyInfo propInfoThree = classType.GetProperty(propThree);
的PropertyInfo propInfoFour = classType.GetProperty(propFour);
的PropertyInfo propInfoFive = classType.GetProperty(propFive);

propInfoOne ???
...............
...............
}
}

凡在propInfo ... S这些信息能够被检索?


< DIV CLASS =h2_lin>解决方案

下面是你如何检查枚举,可为空,primitve和值类型;

  Console.WriteLine(propInfoOne.PropertyType.IsPrimitive); //真
Console.WriteLine(propInfoOne.PropertyType.IsValueType); //假的,值类型结构

Console.WriteLine(propInfoThree.PropertyType.IsEnum); //真

VAR nullableType = typeof运算(可空<>)。MakeGenericType(propInfoThree.PropertyType);
Console.WriteLine(nullableType.IsAssignableFrom(propInfoThree.PropertyType)); //真

请注意值类型和原始类型是不同的东西。原语是简单的映射到类型(例如,布尔> System.Boolean)速记。值类型是按值传递;他们是结构(URE)不是类。


How to differentiate between value-type, nullable value-type, enum, nullable-enum, reference-types through reflection?

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)
        {
            string propOne = "IntegerProp";
            string propTwo = "NullableIntegerProp";
            string propThree = "EnumProp";
            string propFour = "NullableEnumProp";
            string propFive = "ReferenceProp";

            Type classType = typeof(MyClass);

            PropertyInfo propInfoOne = classType.GetProperty(propOne);
            PropertyInfo propInfoTwo = classType.GetProperty(propTwo);
            PropertyInfo propInfoThree = classType.GetProperty(propThree);
            PropertyInfo propInfoFour = classType.GetProperty(propFour);
            PropertyInfo propInfoFive = classType.GetProperty(propFive);

            propInfoOne.???
            ...............
            ...............
        }
    }

Where in the propInfo...s these information can be retrieved?

解决方案

Here is how you check for enum, nullable, primitve and value types;

Console.WriteLine(propInfoOne.PropertyType.IsPrimitive); //true
Console.WriteLine(propInfoOne.PropertyType.IsValueType); //false, value types are structs

Console.WriteLine(propInfoThree.PropertyType.IsEnum); //true

var nullableType = typeof (Nullable<>).MakeGenericType(propInfoThree.PropertyType);
Console.WriteLine(nullableType.IsAssignableFrom(propInfoThree.PropertyType)); //true

Note that value types and primitives are different things. Primitives are simply shorthands that map to types (e.g bool > System.Boolean). Value types are passed by value; they are struct(ure)s not classes.

这篇关于如何价值型,空值类型,枚举,可为空,枚举,引用类型通过反射区分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆