How do I perform explicit operation casting from reflection?

David Basarab picture David Basarab · Sep 8, 2011 · Viewed 7.1k times · Source

I want to use reflection and do either an implicit or explicit coversion using reflection.

Given I have defined Foo this way

public class Foo
{
    public static explicit operator decimal(Foo foo)
    {
        return foo.Value;
    }

    public static explicit operator Foo(decimal number)
    {
        return new Foo(number);
    }

    public Foo() { }

    public Foo(decimal number)
    {
        Value = number;
    }

    public decimal Value { get; set; }

    public override string ToString()
    {
        return Value.ToString();
    }
}

When I run this code

decimal someNumber = 42.42m;

var test = (Foo)someNumber;

Console.WriteLine(test);        // Writes 42.42 No problems

When I try to define a class with Foo as a member type and use reflection to set it. I get the following Exception.

Error     : Object of type 'System.Decimal' cannot be converted to type 'Foo'.
StackTrace:    at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
               at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
               at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
               at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
               at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
               at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
               at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)

Here is the code I use to set the property with reflection

public class FooComposite
{
    public Foo Bar { get; set; }
}

var properties = typeof(FooComposite).GetProperties();

var testFoo = new FooComposite();

foreach(var propertyInfo in properties)
{
    propertyInfo.SetValue(testFoo, 17.17m, null);  // Exception generated on this line
}

Console.WriteLine(testFoo.Bar);  // Never gets here

How can I do this conversion?

Answer

Jamiec picture Jamiec · Sep 8, 2011

Well its really no different from your non-reflection code, you still need to explicitly cast the number to a Foo:

propertyInfo.SetValue(testFoo,(Foo)17.17m, null);

Live example: http://rextester.com/rundotnet?code=BPQ74480

Out of interest I tried a few alternatives.

  1. Make it an implicit cast in Foo - doesnt work, same error Live
  2. Use Convert.ChangeType(17.17m,typeof(Foo)) - also doesnt work. Live