You will have to do the conversion yourself, as the compiler handles the cast in a non-reflection environment. As reflection code basically is evaluating types and objects like the compiler does, you will have to look for a method named op_implicit
with the needed parameters (in your case Int32
) on your object and invoke it. After that, you can invoke the property accessor. A possible way would be the following:
//search for an implicit cast operator on the target type
MethodInfo[] methods = targetType.GetMethods();
foreach(MethodInfo method = source.GetType().GetMethod("op_Implicit"))
{
if (method.Name == "op_Implicit")
{
ParameterInfo[] parameters = method.GetParameters();
if (parameters.Length == 1 && parameters[0].ParameterType == value.GetType())
{
value = method.Invoke(obj,new object[]{value});
break;
}
}
}