I'm dynamically creating an expression tree using values provided in an XML file (i.e. I read strings). The value and the type of the member are read from this file. I'm trying to create a ConstantExpression
of an integer:
XElement expression = GetMyCurrentMember();
//<member type="System.Int32">5</member>
return Expression.Constant(expression.Value, Type.GetType(expression.Attribute("type").Value, false, true));
In the return
statement I'm getting the error Argument types do not match
which, upon inspection, seams about right since I'm passing a string
and saying it's an int
. A simple cast would (probably) solve the problem but that means that I'm loosing the dynamic of the whole system. Instead of int
I could have double
or char
or even a custom type and I don't really want to create a different call or method for every type. Is there a method to "force" the automatic conversion of the input value to the requested type?