none
Get delegate arguments inside delegate

    Question

  • Hi all,

    Could someone please help me to understand how to get all parameters passed to delegate inside delegate itself?

    I have class :

      public class ShopManager : ShopEntities
        {
          public ShopManager getWhere(Func<Object, Object> dataList)
          {
            var x = dataList.???; // how to get arguments?
      
            return this;
          }
      
          public Object getLike(Object dataValue)
          {
            return dataValue;
          }
      }
    


    Then i call it as :

      ShopManager shopManager = new ShopManager()
      var demo = shopManager.getWhere(xxx => shopManager.getLike("DATA"));
    


    The question is : **how to get passed parameters "xxx" and "DATA" inside method getWhere()?**

    Thanks in advance.
    Thursday, October 21, 2010 8:10 AM

Answers

  • If you need to analyze it, you have to take an expression, instead of a delegate.

    public ShopManager getWhere(Expression<Func<Object, Object>> dataList)
    

    You can retrieve the parameter of the lambda using:

    xxx = dataList.Parameters.Single();
    

    and, if the lambda is a method call, the first parameter of that method call using:

    MethodCallExpression call = dataList.Body as MethodCallExpression;
    if(call != null)
    {
     ConstantExpression methodParameter = call.Arguments.FirstOrDefault() as ConstantExpression;
     if (methodParameter != null)
      Data = methodParameter.Value;
    }
    

    Is that what you want? And what if getWhere is called with something else?

    shopManager.getWhere(x => (int)x);
    shopManager.getWhere(x => "some string");
    shopManager.getWhere(x => x.GetHashCode() + 3);
    shopManager.getWhere(x => shopManager.getWhere(shopManager.getLike("DATA")));
    
    • Proposed as answer by Aridane Álamo Thursday, October 21, 2010 11:59 AM
    • Marked as answer by artemiusgreat Saturday, October 23, 2010 8:29 PM
    Thursday, October 21, 2010 11:59 AM
  • I understood that it is not possible to get sign of condition (in this case it is ==)

    dataList.Body.NodeType == ExpressionType.Equal
    • Marked as answer by artemiusgreat Sunday, October 24, 2010 9:46 AM
    Sunday, October 24, 2010 12:43 AM

All replies

  • If you need to analyze it, you have to take an expression, instead of a delegate.

    public ShopManager getWhere(Expression<Func<Object, Object>> dataList)
    

    You can retrieve the parameter of the lambda using:

    xxx = dataList.Parameters.Single();
    

    and, if the lambda is a method call, the first parameter of that method call using:

    MethodCallExpression call = dataList.Body as MethodCallExpression;
    if(call != null)
    {
     ConstantExpression methodParameter = call.Arguments.FirstOrDefault() as ConstantExpression;
     if (methodParameter != null)
      Data = methodParameter.Value;
    }
    

    Is that what you want? And what if getWhere is called with something else?

    shopManager.getWhere(x => (int)x);
    shopManager.getWhere(x => "some string");
    shopManager.getWhere(x => x.GetHashCode() + 3);
    shopManager.getWhere(x => shopManager.getWhere(shopManager.getLike("DATA")));
    
    • Proposed as answer by Aridane Álamo Thursday, October 21, 2010 11:59 AM
    • Marked as answer by artemiusgreat Saturday, October 23, 2010 8:29 PM
    Thursday, October 21, 2010 11:59 AM
  • Thanks for reply Louis,

    The reason i wanted this is to create way to add condition to my DBManager in the way similar to common ObjectQuery.Where(ALIAS => ALIAS.ID == 1).

    I understood that it is not possible to get sign of condition (in this case it is ==), so decided to simplify expression with passing there only left and right sides of expression like here :

    DBManager.Where (ALIAS.ID => 1)

    More of this i need to check range of values but don't want to use common initialization as following : new [] { ... }

    So for this i decided to use :

    DBManager.Where (ALIAS.ID => DBManager.Range (1,2,3,4,5))

    And your answer completely answered my question, THANKS!

    Saturday, October 23, 2010 8:28 PM
  • I understood that it is not possible to get sign of condition (in this case it is ==)

    dataList.Body.NodeType == ExpressionType.Equal
    • Marked as answer by artemiusgreat Sunday, October 24, 2010 9:46 AM
    Sunday, October 24, 2010 12:43 AM