如何使用反射?</enum>将Int16值设置为Nullable <enum>属性

时间:2011-12-22 14:43:33

标签: c# reflection .net-2.0

我需要这样做: -

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。

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

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

怎么做?

修改

我需要进一步的帮助!

Enum.ToObject()无法处理null。

3 个答案:

答案 0 :(得分:9)

好的,在这种情况下你需要使用Enum.ToObject,因为你正在使用反射。但是你还需要打开可以使用它的nullable,Nullable.GetUnderlyingType为你做了。

因此,您需要获得与Type对应的MyEnum

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

然后使用Enum.ToObject生成MyEnum的盒装实例,其中包含您指定的值:

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

所以,把它们放在一起:

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

修改

如果myInteger可以为空,那么您应该使用:

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

答案 1 :(得分:1)

刚投吧?

propertyInfoTwo.SetValue(objectInstance, (MyEnum?)myInteger, null);

答案 2 :(得分:0)

使用int而不是short或

enum MyEnum : Int16 

或将短片转换为int。

<强>更新 试试

short? MyInteger = 1;