C# 将方法名从属性传递到lambda,不带参数

C# 将方法名从属性传递到lambda,不带参数,c#,methods,lambda,delegates,expression,C#,Methods,Lambda,Delegates,Expression,我正在努力实现以下目标: this.helper.Verify(i => i.SomeModule.SomeMethod); 其中: i是一种已知类型 SomeModule是此类型的公共属性 SomeMethod是方法的名称(我假设是作为委托传递的) 我希望能够避免在Verify方法中指定泛型类型 这就是我能走的路: public void Verify<TProp, TResult>( Expression<Func<KnownTyp

我正在努力实现以下目标:

this.helper.Verify(i => i.SomeModule.SomeMethod);
其中:

  • i是一种已知类型
  • SomeModule是此类型的公共属性
  • SomeMethod是方法的名称(我假设是作为委托传递的)
我希望能够避免在
Verify
方法中指定泛型类型

这就是我能走的路:

public void Verify<TProp, TResult>(
            Expression<Func<KnownType, TProp>> moduleExpression,
            Expression<Func<TProp, TResult>> methodExpression)
{
    var moduleIdentifier = (moduleExpression.Body as MemberExpression).Member.Name;
    var methodIdentifier = (methodExpression.Body as MethodCallExpression).Method.Name;

    this.Verify(moduleIdentifier, methodIdentifier, state);
}

有什么方法可以实现我想要的吗?

最后,我得到了一个需要代表的解决方案

public class FakeExtensions
{
    public static void Verify(
        this Fake fake, 
        Expression<Func<KnownType, VoidDelegate>> methodExpression)
    {
        var tools = new ExpressionTools();
        var facilitator = new Verifier(fake, tools);
        facilitator.Verify(methodExpression);
    }

    public static void Verify(
        this Fake fake, 
        Expression<Func<KnownType, VoidBoolDelegate>> methodExpression)
    {
        var tools = new ExpressionTools();
        var facilitator = new Verifier(fake, tools);
        facilitator.Verify(methodExpression);
    }
}
我需要分析一个可以有多个参数的表达式,如:

i => i.Car.Engine.Intake.Open()
我将最后两个参数提取为“模块”和“方法”:

最后,我需要维护一个类,该类具有使用不同委托重载的相同方法

public class FakeExtensions
{
    public static void Verify(
        this Fake fake, 
        Expression<Func<KnownType, VoidDelegate>> methodExpression)
    {
        var tools = new ExpressionTools();
        var facilitator = new Verifier(fake, tools);
        facilitator.Verify(methodExpression);
    }

    public static void Verify(
        this Fake fake, 
        Expression<Func<KnownType, VoidBoolDelegate>> methodExpression)
    {
        var tools = new ExpressionTools();
        var facilitator = new Verifier(fake, tools);
        facilitator.Verify(methodExpression);
    }
}
由于Moq是我们测试框架的一部分,因此可以使用它.IsAny作为方法调用的每个参数的值,wich表示这些值无关紧要:

this.helper.Verify(i => i.SomeModule.SomeMethod(It.IsAny<int>, It.IsAny<bool>, It.IsAny<double>, It.IsAny<SomeType>);
this.helper.Verify(i=>i.SomeModule.SomeMethod(It.IsAny,It.IsAny,It.IsAny,It.IsAny);
我为什么选择代理解决方案 我必须验证对低级别第三方API的一些调用,该API具有多达100个参数的方法。我认为维护委托列表和方法重载的负担在可维护性、可读性和易用性方面得到了回报

namespace Whatever
{
    public delegate void VoidDelegate();
    public delegate void VoidBoolDelegate(bool value);
    // etc...
}
public class FakeExtensions
{
    public static void Verify(
        this Fake fake, 
        Expression<Func<KnownType, VoidDelegate>> methodExpression)
    {
        var tools = new ExpressionTools();
        var facilitator = new Verifier(fake, tools);
        facilitator.Verify(methodExpression);
    }

    public static void Verify(
        this Fake fake, 
        Expression<Func<KnownType, VoidBoolDelegate>> methodExpression)
    {
        var tools = new ExpressionTools();
        var facilitator = new Verifier(fake, tools);
        facilitator.Verify(methodExpression);
    }
}
this.helper.Verify(i => i.SomeModule.SomeMethod(0, false, 0.0, null);
this.helper.Verify(i => i.SomeModule.SomeMethod(It.IsAny<int>, It.IsAny<bool>, It.IsAny<double>, It.IsAny<SomeType>);