Morphex
Morphex

Reputation: 306

Expression.Compile() , Parameter out of scope

I am been messing with Expression Trees and I have hit a setback and can't find why or how to fix this :

        public static Expression GetExp(object caller, string s)
    {
        var callerType = caller.GetType();
        if (s.Contains('('))
        {
            //We are a method call
            var data = s.Split('.');
            var instance = Expression.Parameter(callerType, "obj");
            var currentExpression = (Expression)instance;
            for (int index = 0; index < data.Length - 1; index++)
            {
                var prop = data[index];
                currentExpression = (Expression.PropertyOrField(currentExpression, prop));
            }
            var expLex = Expression.Lambda(currentExpression, instance);
            var compiled = expLex.Compile();
            var result = compiled.DynamicInvoke(caller);
            var propType = result.GetType();
            var propField = Expression.Parameter(propType, "prop");
            var methodExpression = GetMethod(currentExpression,propType, data[data.Length - 1]);
            //What now?


            var fullExp = Expression.Lambda(methodExpression,instance);
            var fullcompiled = fullExp.Compile();
            var final =fullcompiled.DynamicInvoke(caller);

        }
        return null;
    }



    public static MethodCallExpression GetMethod(Expression callingExp,Type owner, string method)
    {
        //parameters = null
        var start = method.IndexOf('(');
        var end = method.LastIndexOf(')');
        string methodName = method.Substring(0, start);
        var amount = end - start; ;
        var methodInfo = owner.GetMethod(methodName,BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
        if (amount <= 1)
        {
            return Expression.Call(callingExp, methodInfo);
        }
        var parameters = methodInfo.GetParameters().Select(v => Expression.Parameter(v.ParameterType, v.Name)).Cast<Expression>().ToList();
        var ara = parameters.Cast<ParameterExpression>().ToArray();
        var result = Expression.Call(callingExp, methodInfo, Expression.Parameter(typeof(int),"something"));
        var fullExp = Expression.Lambda(result);
        var c = fullExp.Compile();
        return result;
    }

Basicly I am getting a Argument out of Scope when I call Compile in the GetMethod call. This only happens if the method has parameters, it works perfectly fine if the method has no parameter.

What am I doing wrong?

Upvotes: 1

Views: 466

Answers (1)

Viru
Viru

Reputation: 2246

I guess you are getting parameter "obj" referenced from scopr but it is not defined error..

Here is how you can fix it. In your GetMethod Method change below line. Pass parameter expression argument from your GetExp method.

public static MethodCallExpression GetMethod(Expression callingExp, Type owner, string method,ParameterExpression param)

Pass this parameter expressoin in below line as argument

Expression.Lambda(result,param);

In your GetMethod change below line. Pass Parameter expression instance which was created above in the same method.

var methodExpression = GetMethod(currentExpression, propType, data[data.Length - 1], instance);

Upvotes: 1

Related Questions