locked
How to get the name of the Func<> parameter RRS feed

  • Question

  • May be the title of the post is not clear but here is what I want :-) :

     I have a method

    public static TResult MyMethod<TResult>(Func<IInterface, TResult> myFunction){....}
    
    

     

     I call it like this for example :

    MyMethod(svc => svc.GetCode(this.Code))
    
    
    

     

    I want to obtain, in my method MyMethod a string or something containing "GetCode" or "svc.GetCode" or even all the syntax "svc => svc.GetCode(this.Code)"

    After some investigations, I didn't see any way to do this, so please tell me I'm wrong and we can do it :-)

    Thanks,

    Friday, April 9, 2010 2:00 PM

Answers

  • Hi Chrizarus,

     

    I try to understand what u really need. But not successful.

    Here is my code based on guess.

     

     

     

    public static TResult MyMethod<TResult>(Expression<Func<IInterface, TResult>> myFunction)
    
            {
    
                var parameterName = myFunction.Parameters.First().Name;
    
                var methodCallExpr = myFunction.Body as MethodCallExpression;
    
                var methodName = methodCallExpr.Method.Name;
    
                var methodArgumentName = methodCallExpr.Arguments.First().ToString();
    
                var entireExp = myFunction.ToString();
    
    
    
              
    
                //just for deamo
    
                return default(TResult);
    
            }

    Hope this help.

     

    Cheers,

    Nyi Nyi

     

     


    Happy programming .net
    • Marked as answer by Figo Fei Monday, April 12, 2010 7:07 AM
    Saturday, April 10, 2010 1:19 PM
  • You need to define the parameter as Expression<Func<IInterface, TResult>>. Then can you its Body property to get a textual representation of the actual lambda passed in.

    If you want to a PropertyInfo for a property expression, you can use the following code:

            private static PropertyInfo AttemptToGetPropertyInfoFromPropertyExpression(Expression<Func<T, object>> expression)
            {
                MemberExpression memberExpression = expression.Body as MemberExpression;
                if (memberExpression != null)
                {
                    return (PropertyInfo) memberExpression.Member;
                }
                    
                return null;
            }
    • Proposed as answer by DennisDoomen Friday, April 9, 2010 6:13 PM
    • Marked as answer by Figo Fei Monday, April 12, 2010 7:07 AM
    Friday, April 9, 2010 4:45 PM

All replies

  • hi,

    Why do want have the name of the function? You can just call the Invoke Method to it.

    If you can put little more explanation, may be we can help better.

    Regards,

    Vinil;

    Friday, April 9, 2010 3:03 PM
  • You need to define the parameter as Expression<Func<IInterface, TResult>>. Then can you its Body property to get a textual representation of the actual lambda passed in.

    If you want to a PropertyInfo for a property expression, you can use the following code:

            private static PropertyInfo AttemptToGetPropertyInfoFromPropertyExpression(Expression<Func<T, object>> expression)
            {
                MemberExpression memberExpression = expression.Body as MemberExpression;
                if (memberExpression != null)
                {
                    return (PropertyInfo) memberExpression.Member;
                }
                    
                return null;
            }
    • Proposed as answer by DennisDoomen Friday, April 9, 2010 6:13 PM
    • Marked as answer by Figo Fei Monday, April 12, 2010 7:07 AM
    Friday, April 9, 2010 4:45 PM
  • Hi Chrizarus,

     

    I try to understand what u really need. But not successful.

    Here is my code based on guess.

     

     

     

    public static TResult MyMethod<TResult>(Expression<Func<IInterface, TResult>> myFunction)
    
            {
    
                var parameterName = myFunction.Parameters.First().Name;
    
                var methodCallExpr = myFunction.Body as MethodCallExpression;
    
                var methodName = methodCallExpr.Method.Name;
    
                var methodArgumentName = methodCallExpr.Arguments.First().ToString();
    
                var entireExp = myFunction.ToString();
    
    
    
              
    
                //just for deamo
    
                return default(TResult);
    
            }

    Hope this help.

     

    Cheers,

    Nyi Nyi

     

     


    Happy programming .net
    • Marked as answer by Figo Fei Monday, April 12, 2010 7:07 AM
    Saturday, April 10, 2010 1:19 PM
  • Thanks both for your answers, it seems to be what I want. I will test it asap and I will revert to you.

     

    Many thanks.

    Monday, April 12, 2010 8:23 AM
  • I have test it and it's exactly what I want :-) many thanks
    Monday, April 12, 2010 12:10 PM