I need to call below ExpFunction with reflection:
class Program
{
static void Main(string[] args)
{
ExpClass<TestClass> obj = new ExpClass<TestClass>();
//without reflection
obj.ExpFunction(f => f.Col);
//with reflection
UsingReflection<TestClass>(obj, typeof(TestClass).GetProperty("Col"));
}
}
public class TestClass
{
public string Col { get; set; }
}
public class ExpClass<T>
{
public string ExpFunction(Expression<Func<T, object>> propertyMap)
{
return "success";
}
}
Here is what I did
static void UsingReflection<T>(ExpClass<T> obj, PropertyInfo Property)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "i");
MemberExpression property = Expression.Property(parameter, Property);
var propertyExpression = Expression.Lambda(property, parameter);
var method = typeof(ExpClass<T>).GetMethod("ExpFunction").MakeGenericMethod(typeof(T));
method.Invoke(obj, new object[] { propertyExpression });
}
But During invoke it says:
Object of type 'System.Linq.Expressions.Expression`1[System.Func`2[ExpressionTest.TestClass,System.String]]'
cannot be converted to type 'System.Linq.Expressions.Expression`1[System.Func`2[ExpressionTest.TestClass,System.Object]]'.
It is probably because ExpFunction accepts Expression<Func<T, object>>. And TestClass.Col is a string.
So how can I do it?
Propertyseems to be the one that needs to be casted. It could be useful if you add the code that calls toUsingReflectionto the question.