Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'casting' with reflection

Consider the following sample code:

class SampleClass
{
    public long SomeProperty { get; set; }
}

public void SetValue(SampleClass instance, decimal value)
{
    // value is of type decimal, but is in reality a natural number => cast
    instance.SomeProperty = (long)value;
}

Now I need to do something similar through reflection:

void SetValue(PropertyInfo info, object instance, object value)
{
    // throws System.ArgumentException: Decimal can not be converted to Int64
    info.SetValue(instance, value)  
}

Note that I cannot assume that the PropertyInfo always represents a long, neither that value is always a decimal. However, I know that value can be casted to the correct type for that property.

How can I convert the 'value' parameter to the type represented by PropertyInfo instance through reflection ?

like image 644
jeroenh Avatar asked Sep 09 '09 10:09

jeroenh


2 Answers

void SetValue(PropertyInfo info, object instance, object value)
{
    info.SetValue(instance, Convert.ChangeType(value, info.PropertyType));
}
like image 163
Thomas Levesque Avatar answered Oct 19 '22 23:10

Thomas Levesque


Thomas answer only works for types that implement IConvertible interface:

For the conversion to succeed, value must implement the IConvertible interface, because the method simply wraps a call to an appropriate IConvertible method. The method requires that conversion of value to conversionType be supported.

This code compile a linq expression that does the unboxing (if needed) and the conversion:

    public static object Cast(this Type Type, object data)
    {
        var DataParam = Expression.Parameter(typeof(object), "data");
        var Body = Expression.Block(Expression.Convert(Expression.Convert(DataParam, data.GetType()), Type));

        var Run = Expression.Lambda(Body, DataParam).Compile();
        var ret = Run.DynamicInvoke(data);
        return ret;
    }

The resulting lambda expression equals to (TOut)(TIn)Data where TIn is the type of the original data and TOut is the given type

like image 43
Rafael Avatar answered Oct 19 '22 22:10

Rafael