none
variable 'Test' of type 'ITest' referenced from scope '', but it is not defined RRS feed

  • Question

  • Hi,

    i have interface defined below.

     public interface ITest
     {
           void Column1();
     }

    Class below implementing  the interface

    public class Test:ITest
    {
             public void Column1()
            {
                Console.WriteLine("Writing from Column1");
            }
    }

    and i am trying to call this method using Linqexpression as below.


    ParameterExpression pe = Expression.Parameter(typeof(ITest), "Test");
    MethodInfo minfo = typeof(ITest).GetMethod("Column1", System.Type.EmptyTypes);
    Expression left = Expression.Call(pe, minfo);
    Expression.Lambda(left).Compile();

    the last line is throwing error

    variable 'Test' of type 'ITest' referenced from scope '', but it is not defined.

    i searched around but not able to find reason. Can someone please throw me some ideas. Any help much appreciated. Thanks


    Wednesday, August 10, 2016 5:01 PM

Answers

  • Hi Suresh Enjetipati,

    According to your description and related code, I create a demo and reproduce your issue on my side, it seems that we could use Action<T> to delegate your Test class like this:

     ParameterExpression pe = Expression.Parameter(typeof(ITest), "Test");
     MethodInfo minfo = typeof(ITest).GetMethod("Column1", System.Type.EmptyTypes);
     MethodCallExpression left = Expression.Call(pe, minfo);
     Expression.Lambda<Action<Test>>(left, new List<ParameterExpression>() { pe }).Compile();

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, August 11, 2016 2:35 AM
    Moderator

All replies

  • Hi Suresh Enjetipati,

    According to your description and related code, I create a demo and reproduce your issue on my side, it seems that we could use Action<T> to delegate your Test class like this:

     ParameterExpression pe = Expression.Parameter(typeof(ITest), "Test");
     MethodInfo minfo = typeof(ITest).GetMethod("Column1", System.Type.EmptyTypes);
     MethodCallExpression left = Expression.Call(pe, minfo);
     Expression.Lambda<Action<Test>>(left, new List<ParameterExpression>() { pe }).Compile();

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, August 11, 2016 2:35 AM
    Moderator
  • Thanks  Cole Wu. it worked.
    Thursday, August 11, 2016 1:40 PM