简体   繁体   English

为什么我不能在T4模板中的类成员中访问实用程序方法?

[英]Why can't I access the utility methods in class members in T4 templates?

I'm trying to write a class in a T4 template that uses the Text Template Utility Methods (like WriteLine, PushIndent, PopIndent). 我正在尝试使用文本模板实用程序方法 (如WriteLine,PushIndent,PopIndent)在T4模板中编写一个类。 However, if I try to call these methods in my class, I'll get a compiler error stating 但是,如果我尝试在我的类中调用这些方法,我将收到编译器错误说明

Compiling transformation: Cannot access a non-static member of outer type 'Microsoft.VisualStudio.TextTemplating.TextTransformation' via nested type 'Microsoft.VisualStudio.TextTemplatingBF13B4A5FBA992E5EF81A8A7A4EACCAC3F7698E169D0F7825ED4F22A28C7C52C2B766D83F4C5ACA13E0DE0B3152B6D966E34EB8C5FC677E145F55BE0485406EC.GeneratedTextTransformation.ClassGenerator' 编译变换:无法访问经由嵌套类型“Microsoft.VisualStudio.TextTemplatingBF13B4A5FBA992E5EF81A8A7A4EACCAC3F7698E169D0F7825ED4F22A28C7C52C2B766D83F4C5ACA13E0DE0B3152B6D966E34EB8C5FC677E145F55BE0485406EC.GeneratedTextTransformation.ClassGenerator”外类型“Microsoft.VisualStudio.TextTemplating.TextTransformation”的非静态成员

A MCVE (minimal complete verifiable example) would look like this: MCVE(最小完整可验证示例)如下所示:

<#+
public void FunctionSample()
{
    WriteLine("Hello"); // This works fine
}

public class SampleClass
{
    public static void StaticMethodSample()
    {
        WriteLine("Hello"); // This does not compile
    }

    public void InstanceMethodSample()
    {
        WriteLine("Hello"); // This does not compile either
    }
}
#>

Is there any way to access these utility methods within a class scope or do I have to use free functions? 有没有办法在类范围内访问这些实用程序方法,还是我必须使用自由函数?

(I'm running on Visual Studio 2015 Community) (我在Visual Studio 2015社区上运行)

As PetSerAl pointed out in the comments, you can just call the T4 Utility Methods in any "free function" in a Class Feature Control Block because they are inherited from the TextTransformation base class, ie these free functions are not exactly free, they are methods within the scope of an implicitly created class that derives from TextTransformation . 正如PetSerAl在评论中指出的那样,您可以在类功能控制块中的任何“自由函数”中调用T4实用程序方法,因为它们是从TextTransformation基类继承的,即这些自由函数不是完全免费的,它们是方法在从TextTransformation派生的隐式创建的类的范围内。 That's why you can also access this in these functions. 这就是为什么你也可以在这些功能中访问this

So if you want to use a utility method in another class you defined in a T4 template (this class is actually a nested child class), you have to pass in a reference of TextTransformation to it, eg like this: 因此,如果要在T4模板中定义的另一个类中使用实用程序方法(此类实际上是嵌套的子类),则必须将TextTransformation的引用传递给它,例如:

<#
var @object = new SampleClass(this); // Pass 'this' (TextTransformation) to the constructor
@object.SayHello();
#>

<#+
public class SampleClass // This is actually a nested child class in T4 templates
{
    private readonly TextTransformation _writer;

    public SampleClass(TextTransformation writer)
    {
        if (writer == null) throw new ArgumentNullException("writer");
        _writer = writer;
    }

    public void SayHello()
    {
        _writer.WriteLine("Hello");
    }
}
#>

More information can be found in the MSDN library . 可以在MSDN库中找到更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM