如何使用Reflection.SetValue时提供转换? [英] How to provide conversion when using Reflection.SetValue?

查看:148
本文介绍了如何使用Reflection.SetValue时提供转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不伪装成一个int一类,因此它已经超负荷各个运营商;

I have a class that pretends to be an int, so it has overloaded the various operators;

public class MyId
{
    int value;
    public virtual int Value
    {
        get { return this.value; }
        set { this.value = value; }
    }

    public MyId(int value)
    {
        this.value = value;
    }


    public static implicit operator MyId(int rhs)
    {
        return new MyId(rhs);
    }

    public static implicit operator int(MyId rhs)
    {
        return rhs.Value;
    }


}



然而,当我用这样的代码

However, when I use code like

PropertyInfo.SetValue(myObj, 13, null)
OR
MyId myId = 13;
int x = Convert.ToInt32(myId);
IConvertible iConvertible = x as IConvertible;
iConvertible.ToType(typeof(MyId), CultureInfo.CurrentCulture);



我得到无效的转换。我很纳闷,这两个调用似乎试图调用转换上会失败,因为INT INT不明白类型MYID(即使所有的赋值运算符是那里)。对于这个解决办法的任何想法,我敢肯定,我必须失去了一些愚蠢的事?

I get invalid cast. I'm puzzled, both calls seem to attempt to call convert on the int which will fail because int doesn't understand the type MyId (even though all the assignment operators are there). Any ideas of a workaround for this, I'm sure I must be missing something stupid?

推荐答案

隐式转换是一个C#构造而无法通过反思。此外,通过反射设置字段或属性意味着你的必须的提供相应类型的锋线。您可以尝试通过使用自定义类型转换器(或自定义转换其他方式),以帮助在运行时使用反射之前转换你的类型来规避这一点。这里是一个类型转换器实现的一个粗略的例子

Implicit conversions are a C# construct and are not available through reflection. Additionally, setting a field or property through reflection means that you must provide the appropriate type up front. You can attempt to circumvent this by using a custom TypeConverter (or some other means of custom conversion) to help convert your types at runtime prior to using reflection. Here's a rough example of a TypeConverter implementation.

public class MyIdTypeConverter : TypeConverter
{                
    public override object ConvertFrom(ITypeDescriptorContext context,
                                       System.Globalization.CultureInfo culture,
                                       object value)
    {   
        if (value is int)
            return new MyId((int)value);
        else if (value is MyId)
            return value;
        return base.ConvertFrom(context, culture, value);
    }               
}

下面是我们将试图设置的类型自定义的属性。

Here's the type that we would be trying to set the Custom property on.

public class Container
{
    [TypeConverter(typeof(MyIdTypeConverter))]
    public MyId Custom { get; set; }                
}



调用它的代码会检查属性和执行转换时间提前后,它可以调用的SetValue

var instance = new Container();
var type = typeof(Container);
var property = type.GetProperty("Custom");

var descriptor = TypeDescriptor.GetProperties(instance)["Custom"];
var converter = descriptor.Converter;                
property.SetValue(instance, converter.ConvertFrom(15), null);

这篇关于如何使用Reflection.SetValue时提供转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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