C# 将委托方法存储为类的成员

C# 将委托方法存储为类的成员,c#,delegates,C#,Delegates,我正在进行编码的ui测试,基本上是ui的单元测试,我创建了一个TestObject类,该类在实例化它的TestMethod中存储了一个针对自身执行的断言列表 public class TestObject { public string urlToTest; public List<Assertion> assertions; } public class Assertion { public List<SearchPropertyExpression

我正在进行编码的ui测试,基本上是ui的单元测试,我创建了一个
TestObject
类,该类在实例化它的TestMethod中存储了一个针对自身执行的断言列表

public class TestObject {
    public string urlToTest;
    public List<Assertion> assertions;
}

public class Assertion {
    public List<SearchPropertyExpression> searchPropertyExpressions;
    public Action assertMethod;
    public string expectedValue; // <-- this works fine if I'll always call a method like AreEqual() where it has an expected value, but what if I want to store a method in assertMethod that has different arguments???
}

public class SearchPropertyExpression {
    public string expression;
    public string value;
}

我认为我真正的问题是,这里有一个设计模式可以真正帮助我,但我对设计模式不太熟悉。

您的
assertMethod
委托属于
Action
类型,它表示返回类型为void且没有参数的方法,例如
void Foo()

Assert.AreEqual
has,最普遍的是
Assert.AreEqual(预期对象,实际对象)
。我建议您使用此选项,并相应地更改您的代表:

Action<Object, Object> assertMethod;
动作资产方法;

如果希望您的委托指向任何方法定义,可以执行类似操作:-

public delegate void MyAction(params object[] args);
public class Assertion
{
    public List<PropertyExpression> propertyExpressions;
    public MyAction assertMethod;
}
public void Test()
{
   var asser = new Assertion()
                    {
                        assertMethod = (vals) =>Assert.AreEqual(vals[0],vals[1]);
                        propertyExpressions = null
                    };
   var asser2 = new Assertion()
                    {
                        assertMethod = (vals)=>Assert.AreEqual((string)vals[0],(string)vals[1],(bool)vals[2]);
                        propertyExpressions = null
                    };

    asser.assertMethod(1, 1);//calling object,object overload
    asser2.assertMethod("ab", "cd", true);//calling string,string,bool overload
}
public委托void MyAction(参数对象[]args);
公共类断言
{
公共列表属性表达式;
公共行动资产评估方法;
}
公开无效测试()
{
var asser=新断言()
{
assertMethod=(VAL)=>Assert.AreEqual(VAL[0],VAL[1]);
propertyExpressions=null
};
var asser2=新断言()
{
assertMethod=(VAL)=>Assert.AreEqual((字符串)VAL[0],(字符串)VAL[1],(布尔)VAL[2]);
propertyExpressions=null
};
asser.assertMethod(1,1);//调用对象,对象重载
asser2.assertMethod(“ab”,“cd”,true);//调用字符串,字符串,bool重载
}

您是否拥有要在其中存储方法的类?看起来不像,但我想我会检查确认。好的,这很简单,我想我会给你一个小演示,给我一点时间谢谢丹尼尔。我突然想到了一件事,因为我可以为每个方法调用传递任何带有可能不同参数的Assert方法,所以如何在TestMethod中动态调用assertMethod,以便将正确的参数传递给每个Assert方法?谢谢BuggyCoder。问题是,我仍然需要在实际调用委托时传递参数,因为我不知道在运行时实际调用的是什么方法,所以在调用委托时我不能传递参数。这有什么意义吗?我想我需要在实例化
TestObject
时指定参数,我想???看,我们只在调用assertMethod时传递参数,在构建委托(MyAction)时,我们使用Lambda构建AreEquals。。。您在lambda中看到的参数只是告诉assertmethod它的定义/主体是什么样子的..也许我从错误的角度来处理这个问题。我目前正在遍历
TestObject
中的所有断言对象,并尝试在每个断言对象中执行断言方法。问题是,当实际调用
assertMethod
时,无法知道(至少我认为没有)参数是什么。我对上面的代码进行了更改,请参阅代码底部附近的
x.assertMethod
上的注释
public delegate void MyAction(params object[] args);
public class Assertion
{
    public List<PropertyExpression> propertyExpressions;
    public MyAction assertMethod;
}
public void Test()
{
   var asser = new Assertion()
                    {
                        assertMethod = (vals) =>Assert.AreEqual(vals[0],vals[1]);
                        propertyExpressions = null
                    };
   var asser2 = new Assertion()
                    {
                        assertMethod = (vals)=>Assert.AreEqual((string)vals[0],(string)vals[1],(bool)vals[2]);
                        propertyExpressions = null
                    };

    asser.assertMethod(1, 1);//calling object,object overload
    asser2.assertMethod("ab", "cd", true);//calling string,string,bool overload
}