C# 将CakeContext传递给另一个.cake文件

C# 将CakeContext传递给另一个.cake文件,c#,cakebuild,C#,Cakebuild,我用蛋糕0.21.1.0 我的build.cake脚本加载另一个.cake脚本:测试.cake 在tests.cake中,我有一个名为TestRunner的类TestRunner有一个名为RunUnitTests()的方法,它使用VSTest方法执行单元测试 在build.cake中,我创建了几个TestRunner的实例。每当我在任何一个实例上调用RunUnitTests()方法时,我都会看到以下错误消息: error CS0120: An object reference is requir

我用蛋糕0.21.1.0

我的
build.cake
脚本加载另一个
.cake
脚本:
测试.cake

tests.cake
中,我有一个名为
TestRunner
的类
TestRunner
有一个名为
RunUnitTests()
的方法,它使用
VSTest
方法执行单元测试

build.cake
中,我创建了几个
TestRunner
的实例。每当我在任何一个实例上调用
RunUnitTests()
方法时,我都会看到以下错误消息:

error CS0120: An object reference is required for the non-static field, method, or property 'VSTest(IEnumerable<FilePath>, VSTestSettings)'
error CS0027: Keyword 'this' is not available in the current context
build.cake
中,我的任务之一执行以下操作:

TestRunner testRunner = TestRunnerAssemblies[testRunnerName];
testRunner.RunUnitTests(this);
其中,
testrunnerasemblies
tests.cake
中的只读字典,
testRunnerName
是先前定义的变量。(在
build.cake
中,我插入了
#l“tests.cake”

现在我看到这个错误消息:

error CS0120: An object reference is required for the non-static field, method, or property 'VSTest(IEnumerable<FilePath>, VSTestSettings)'
error CS0027: Keyword 'this' is not available in the current context
我做错了什么

编辑:

没关系,我需要学习如何更仔细地阅读。我没有像devlead最初建议的那样传入
这个
,而是传入了
上下文
。现在可以毫无问题地调用
RunUnitTests
方法。

如果
RunUnitTests()
是一个静态方法或在类中,您需要将上下文作为参数传递给它,就像
RunUnitTests(ICakeContext context)
一样,因为它是一个不同的作用域

然后,您可以在该方法上执行别名作为扩展

例如:

RunUnitTests(Context);

public static void RunUnitTests(ICakeContext context)
{
    context.VSTest(...)
}
类的示例:

Task("Run-Unit-Tests")
    .Does(TestRunner.RunUnitTests);

RunTarget("Run-Unit-Tests");


public static class TestRunner
{
    public static void RunUnitTests(ICakeContext context)
    {
        context.VSTest("./Tests/*.UnitTests.dll");
    }
}

谢谢你的帮助,德夫里德!我已经对我的帖子做了一些更新,以回应您的回复。如果您阅读了我的第一个示例,请不要发送
发送
上下文
。谢谢您的回复。我需要学习如何更仔细地阅读:-)太好了,太好了,它被分类了;)不要使用
this
,而是使用
Context
,如下所示:以及@devlead示例中所示