emregon
emregon

Reputation: 418

Call a method with parameter type Expression<Func<T, object>> using reflection

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?

Upvotes: 1

Views: 1647

Answers (2)

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73452

There are two problems, you're not casting the property to Object and you call MakeGenericMethod on a method which is not generic at all.

static void UsingReflection<T>(ExpClass<T> obj, PropertyInfo Property)
{
    ParameterExpression parameter = Expression.Parameter(typeof(T), "i");

    MemberExpression property = Expression.Property(parameter, Property);
    var castExpression = Expression.TypeAs(property, typeof(object));
    var propertyExpression = Expression.Lambda(castExpression, parameter);

    var method = typeof(ExpClass<T>).GetMethod("ExpFunction");

    method.Invoke(obj, new object[] { propertyExpression });
}

Upvotes: 2

aush
aush

Reputation: 2108

The ExpFunction is not generic, so you shouldn't .MakeGenericMethod(typeof(T)) it.

Upvotes: 1

Related Questions