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?
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.
implicit
cast in Foo
- doesnt work, same error Live
Convert.ChangeType(17.17m,typeof(Foo))
- also doesnt work. Live
I needed functionality like Ted H, but I implemented it like this:
var cast = typeof(dest).GetMethod("op_Explicit", new Type[] { typeof(source) });
var result = cast.Invoke(null, new object[] {value});
Edit: I needed a more evolved version recently, and this is what I came up with. Be aware it does not cover all available conversions.
private static object DynamicCast(object source, Type destType) {
Type srcType = source.GetType();
if (srcType == destType) return source;
var paramTypes = new Type[] { srcType };
MethodInfo cast = destType.GetMethod("op_Implicit", paramTypes);
if (cast == null) {
cast = destType.GetMethod("op_Explicit", paramTypes);
}
if (cast != null) return cast.Invoke(null, new object[] { source });
if (destType.IsEnum) return Enum.ToObject(destType, source);
throw new InvalidCastException();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With