如何将Int16值设置为Nullable< Enum>。属性使用反射? [英] How to set an Int16 value to a Nullable<Enum> property using reflection?

查看:136
本文介绍了如何将Int16值设置为Nullable< Enum>。属性使用反射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这样做:-

MyClass.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace set_property__Enum_To_Nullable_Enum
{
    public enum MyEnum : short
    {
        One, 
        Two, 
        Three 
    }

    public class MyClass
    {
        public string MyStringProperty { get; set; }
        public MyEnum? MyEnumProperty { get; set; }

        public void ShowAll(string message)
        {
            Console.WriteLine(message);            
            Console.WriteLine("String   = " + MyStringProperty);
            Console.WriteLine("Enum   = " + MyEnumProperty.Value);
        }
    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace set_property__Enum_To_Nullable_Enum
{
    class Program
    {
        static void Main(string[] args)
        {
            //Names
            string myAssemblyName = "MyAssembly";
            string myNamespaceName = "set_property__Enum_To_Nullable_Enum";
            string myClassName = "MyClass";
            string propertyNameOne = "MyStringProperty";
            string propertyNameTwo = "MyEnumProperty";

            //Data            
            string myString = "Hello World!";
            short myInteger = 1;

            //Preprocessing
            Assembly myAssmbly = Assembly.Load(myAssemblyName);
            Type myType = myAssmbly.GetType(myNamespaceName + "." + myClassName);

            //Create class-instance
            object objectInstance = Activator.CreateInstance(myType);

            //Set property-values
            PropertyInfo propertyInfoOne = myType.GetProperty(propertyNameOne);
            propertyInfoOne.SetValue(objectInstance, myString, null);

            PropertyInfo propertyInfoTwo = myType.GetProperty(propertyNameTwo);
            propertyInfoTwo.SetValue(objectInstance, myInteger, null);//<---------------

            //Call method
            myType.InvokeMember("ShowAll",
                                        BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
                                        null,
                                        objectInstance,
                                        new object[] { "My name is Khan" });

            string end = string.Empty;
        }
    }
}

但是Int32值不会自动转换为MyEnum。

But Int32 value is not automatically converted to MyEnum.

在特定行上,正在生成异常。

At the specific line, an exception is being generated.

Object of type 'System.Int16' cannot be converted to type 'System.Nullable`1[set_property__Enum_To_Nullable_Enum.MyEnum]'.

该怎么做?

我需要进一步的帮助!

Enum.ToObject()无法处理空值。

Enum.ToObject() can not handle null.

推荐答案

好的,在这种情况下,您需要使用 Enum.ToObject ,因为重新使用反射。但是,您还需要解开可为null的值以使用它,这是 Nullable.GetUnderlyingType 为您服务的。

OK, you need to use Enum.ToObject in this case, since you're using reflection. But you also need to unwrap the nullable to use that, which Nullable.GetUnderlyingType does for you.

您需要获取与 MyEnum 对应的 Type

Type nullableEnumType = propertyInfoTwo.PropertyType;
Type enumType = Nullable.GetUnderlyingType(nullableEnumType);

然后使用 Enum.ToObject 生成一个具有指定值的 MyEnum 的带框实例:

then use Enum.ToObject to produce a boxed instance of MyEnum with the value you specify:

object enumValue = Enum.ToObject(enumType, myInteger);

因此,将它们放在一起:

So, putting it all together:

object enumValue = Enum.ToObject(Nullable.GetUnderlyingType(propertyInfoTwo.PropertyType), myInteger);
propertyInfoTwo.SetValue(objectInstance, enumValue, null);

编辑:

如果 myInteger 本身可为空,则应使用:

if myInteger is nullable, itself, you should use:

object enumValue = 
    myInteger.HasValue
        ? Enum.ToObject(Nullable.GetUnderlyingType(propertyInfoTwo.PropertyType), myInteger.Value);
        : null;

这篇关于如何将Int16值设置为Nullable&lt; Enum&gt;。属性使用反射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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