通过反射设置属性时的类型转换问题 [英] Type conversion issue when setting property through reflection

查看:62
本文介绍了通过反射设置属性时的类型转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个类型为 long?的属性,其中填充了 int

We have a property of type long? that gets filled with an int.

当我直接将属性设置为 obj.Value = v; 时,此方法很好,但是当我尝试通过反射<$设置属性时c $ c> info.SetValue(obj,v,null); 它给了我一个以下异常:

This works fine when i just set the property directly obj.Value = v; but when i try and set the property through reflection info.SetValue(obj, v, null); it gives me a the following exception:


类型'System.Int32'的对象不能转换为类型'System.Nullable`1 [System.Int64]'。

Object of type 'System.Int32' cannot be converted to type 'System.Nullable`1[System.Int64]'.

这是一个简化的场景:

    class TestClass
    {
        public long? Value { get; set; }
    }

    [TestMethod]
    public void TestMethod2()
    {
        TestClass obj = new TestClass();
        Type t = obj.GetType();

        PropertyInfo info = t.GetProperty("Value");
        int v = 1;

        // This works
        obj.Value = v;

        // This does not work
        info.SetValue(obj, v, null);
    }

为什么通过设置属性时不起作用反射在直接设置属性时有效吗?

Why does it not work when setting the property through reflection while it works when setting the property directly?

推荐答案

查看全文:如何使用反射来设置属性的值?

完整代码,如果您要为可空类型设置值

full code if you are setting value for nullable type

public static void SetValue(object inputObject, string propertyName, object propertyVal)
{
    //find out the type
    Type type = inputObject.GetType();

    //get the property information based on the type
    System.Reflection.PropertyInfo propertyInfo = type.GetProperty(propertyName);

    //find the property type
    Type propertyType = propertyInfo.PropertyType;

    //Convert.ChangeType does not handle conversion to nullable types
    //if the property type is nullable, we need to get the underlying type of the property
    var targetType = IsNullableType(propertyType) ? Nullable.GetUnderlyingType(propertyType) : propertyType;

    //Returns an System.Object with the specified System.Type and whose value is
    //equivalent to the specified object.
    propertyVal = Convert.ChangeType(propertyVal, targetType);

    //Set the value of the property
    propertyInfo.SetValue(inputObject, propertyVal, null);

}
private static bool IsNullableType(Type type)
{
    return type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>));
}






您需要转换价值这样,即您需要将值转换为如下所示的属性类型


you need to convert value like this i.e you need to convert value to your property type like as below

PropertyInfo info = t.GetProperty("Value");
object value = null;
try 
{ 
    value = System.Convert.ChangeType(123, 
        Nullable.GetUnderlyingType(info.PropertyType));
} 
catch (InvalidCastException)
{
    return;
}
propertyInfo.SetValue(obj, value, null);

您需要执行此操作,因为您无法将任意值转换为给定类型...因此您需要像这样转换

you need to do this because you cannot convert any arbirtary value to given type...so you need to convert it like this

这篇关于通过反射设置属性时的类型转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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