2010-07-22 63 views
2

我希望能够做到这一点:如何将lambda表达式作为返回方法的参数?

var test = SomeMethod(s => s.SomeMethod); 

我可以通过使方法签名看起来像这样使它与属性的作用:

SomeMethod<TProperty>(Expression<Func<T, TProperty>> expression) 

我怎样才能使它与方法的工作?我知道这很简单,我只是想念一些小事。

+0

什么s.SomeMethod'的'类型签名? – stakx 2010-07-22 18:33:19

+3

你到底想要做什么? :)我很想看到答案... – 2010-07-22 18:33:52

+0

他正在尝试做的事情就像'Func >' – 2010-07-22 18:37:36

回答

1

起订量,要求该方法的参数接近此用特殊的“标记”来排除对象,如:

test.Setup(m => m.SomeMethod(It.IsAny<int>())); 

顺便说一句,这使得起订量解决方法重载(如果你一个方法名称不明确唐没有任何所需参数的概念。)他们正在进一步研究并使用它来根据标准实际匹配参数,目的是模拟单元测试的对象。 source code is available,可能会给你一些想法。他们用表情树做各种有趣的诡计。


这里是基于Moq的源上的示例(遭出了一点,但示出了如何方法可以从表达中提取):

internal static void ExtractMethod<T>(Expression<Action<T>> expression) 
     where T : class 
    { 
      var methodCall = expression.ToMethodCall(); 
      var method = methodCall.Method; 
      var args = methodCall.Arguments.ToArray(); 
    } 

直接从Moq的源:

/// <summary> 
    /// Casts the body of the lambda expression to a <see cref="MethodCallExpression"/>. 
    /// </summary> 
    /// <exception cref="ArgumentException">If the body is not a method call.</exception> 
    public static MethodCallExpression ToMethodCall(this LambdaExpression expression) 
    { 
     Guard.NotNull(() => expression, expression); 

     var methodCall = expression.Body as MethodCallExpression; 
     if (methodCall == null) 
     { 
      throw new ArgumentException(string.Format(
       CultureInfo.CurrentCulture, 
       Resources.SetupNotMethod, 
       expression.ToStringFixed())); 
     } 

     return methodCall; 
    } 

这将与您的代码一起工作,但您必须传递“虚拟”参数以允许编译器创建表达式。所以,如果你有:

public void SomeMethod(int value, string text) {} 

然后,你把它作为:

ExtractMethod(s => s.SomeMethod(0, null)); 
0

是这样的?

public SimpleCommand(Predicate<object> canExecuteDelegate, Action<object> executeDelegate) 
    { 
     CanExecuteDelegate = canExecuteDelegate; 
     ExecuteDelegate = executeDelegate; 
    } 

您将需要使用Predicate或Action来指定函数签名。

+0

绝对不是第一个参数,因为Predicate返回true或false ...我不认为第二个工程,除非我做错了... 在我的SomeMethod方法中,我需要能够访问有关方法本身被传入 – 2010-07-22 18:35:24

+0

然后我错过了那个部分所以你需要一个表达式来获取函数的名字(或者其他的东西)? – 2010-07-22 18:39:22

+0

是的,基本上我需要名字...和关于参数的信息它需要,如果可能的话(虽然没有必要,但atm) – 2010-07-22 18:40:32

0
class Program 
    { 
     public class Test 
     { 
      public bool SomeMethod(string test) 
      { 
       return true; 
      } 
     } 

     static void Main(string[] args) 
     { 

      Test testObj = new Test(); 


      Func<string, bool> rule1 = AddRule(testObj, x => x.SomeMethod); 

      bool rsult = rule1("ttt"); 

     } 

     static Func<string, bool> AddRule<T>(T obj, Func<T,Func<string, bool>> func) 
     { 
      return func(obj); 
     } 


} 
+0

这样做的方式需要我知道方法签名,正确吗?这不是一种可能性 – 2010-07-22 18:46:28

+0

是的,没有办法知道方法签名,因为他们可能是无限的可能性 – 2010-07-22 18:47:49

+0

@Max,棘手的问题;) – 2010-07-22 18:48:13

1

是这样的东西你在找什么?

DoSomethingWithAction<T>(Func<T, Action> actionRetriever) { } 
DoSomethingWithFunction<T, TResult>(Func<T, Func<TResult>> functionRetriever) { } 

你会叫这些名字:

DoSomethingWithAction<ObjectWithMethod>(obj => obj.Method); 
DoSomethingWithFunction<ObjectWithProperty>(obj => obj.Property); 
0

我想,有没有这样做,而不指定方法签名的方法:试想重载方法:

void SomeMethod() { } 
int SomeMethod(int a, int b) { return 0; } 

// ... 

var test = TestMethod(s => s.SomeMethod); 

会如何编译器可能知道,你想测试哪种方法?

相关问题