How to set an Int16 value to a Nullable<Enum> property using reflection?

一曲冷凌霜 提交于 2019-12-22 08:59:12

问题


I need to do this:-

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;
        }
    }
}

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]'.

How to do it?

Edit

I need further help please!

Enum.ToObject() can not handle null.


回答1:


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.

So, you need to get the Type corresponding to MyEnum:

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

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);

EDIT:

if myInteger is nullable, itself, you should use:

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



回答2:


Just cast it?

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



回答3:


use int instead of short or

enum MyEnum : Int16 

or convert the short to an int.

update: try

short? MyInteger = 1;


来源:https://stackoverflow.com/questions/8605457/how-to-set-an-int16-value-to-a-nullableenum-property-using-reflection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!