我敢肯定论坛上某个地方已经有答案了,但到目前为止我还没有找到。根据此示例,我将匿名方法与委托结合使用,以便使用具有不同参数但返回类型相同的不同方法都用作函数参数:
public delegate TestCaseResult Action();
...
[TestDescription("Test whether the target computer has teaming configured")]
public TestCaseResult TargetHasOneTeam()
{
// do some logic here and return
// TestCaseResult
}
[TestDescription("Test whether the target computer has the named team configured")]
public TestCaseResult TargetHasNamedTeam(string teamName)
{
// do some logic here and return
// TestCaseResult
}
...
public static void TestThat(TestCaseBase.Action action)
{
TestCaseResult result = action.Invoke();
// I want to get the value of the TestDescription attribute here
}
...
// usage
TestThat(() => TargetHasOneTeam());
TestThat(() => TargetHasNamedTeam("Adapter5"));
从示例中可以看出,我真的很希望能够从 TestThat() 函数中获取 TestDescriptionAttribute 属性。我已经浏览了包含我的方法的 Action 参数,但无法“找到”我的 TargetHasOneTeam() 方法。