T4 - 错误60:一个字段初始不能引用非静态字段,方法或属性 [英] T4 - Error 60: A field initializer cannot reference the non-static field, method, or property

查看:127
本文介绍了T4 - 错误60:一个字段初始不能引用非静态字段,方法或属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着用从微软文档

在我的 .TT 文件的开头,我把:

 <#@模板语言=C#hostSpecific =真#>
<#@输出扩展=CS#>
<#@导入命名空间=System.IO#>
 

然后我尝试从我的previosly转换文件中获取一些变量:

 <#+
    字符串MYFILE = File.ReadAllText(Host.ResolvePath(AssemblyInfo1.cs));
#>
 

在上面的行,我得到的错误。如果该行被注释掉一切正常。 错误输出是:

  

错误60:一个字段初始不能引用非静态字段,   方法或属性

解决方案

它使用一个&LT做;#+#>块而不是使用< ##>。 <#+#>块类功能控制块,并且只能包含code,一个类(即性能/功能/类级别的变量)内工作。请参阅 MS文档更多信息有关如何工作,但基本上都是在执行您的模板转化为类,并在另一个应用程序域中执行和结果的文本保存到一个文件中。基本上你的code试图做的是访问类的局部变量在这样的初始化语句:

 公共类T4GeneratedClass
{
    公共对象主机{获得;组; }

    私人字符串MYFILE = File.ReadAllText(this.Host.ResolvePath(AssemblyInfo1.cs));
}
 

这是无效的code,后=语句是一个尝试使用它在被定义同一类属性(this.Host)设置值MYFILE。File.ReadAllText是OK,因为这是一个静态函数,同种类的一部分。

鉴于应该工作,因为他们正在创建的类,然后从这些功能的主机属性(this.Host)可在体内新的功能,其他的答案。

有关您的code工作作为是,来自以下更改控制块类型;#+#>到< ##>。这将在主函数创建MYFILE变量,将有机会获得主机属性。

如果您想了解更多关于T4是如何运作的,你可以下载我扩展,我有一个功能(它的社区版的一部分,所以你可以免费使用它)会告诉你,是生成和使用的T4框架实际上类,它应有助于阐明一些轻什么T4在做什么,为什么这是行不通的。

I try to use the solution from Microsoft documentation.

At the beginning of my .tt file I put:

<#@ template language="C#" hostSpecific="True" #>
<#@ output extension="cs" #>
<#@ import namespace="System.IO" #>

Then I try to get some variables from my previosly transformed file:

<#+
    string myFile = File.ReadAllText(Host.ResolvePath("AssemblyInfo1.cs"));
#>

In the line above I get error. If the line is commented everything is OK. Error output is:

Error 60: A field initializer cannot reference the non-static field, method, or property

解决方案

It has do with using a <#+ #> block instead of using a <# #>. <#+ #> blocks are class feature control blocks and can only contain code that works within a class (ie properties/functions/class level variables). See the MS docs for more info on how this works but basically at execution your template is transformed into a class and executed in another app domain and the results as text are saved to a file. Essentially what your code was trying to do is access a local variable of the class in an initialization statement like this:

public class T4GeneratedClass 
{
    public object Host { get; set; }

    private string myFile = File.ReadAllText(this.Host.ResolvePath("AssemblyInfo1.cs"));
}

This is not valid code, the statement after = is a trying to set the value "myFile" using a property (this.Host) on the same class it is being defined in. "File.ReadAllText" is ok because that is a static function and not part of the same class.

The other answers given should work because they are creating new functions on the class and from the body of those functions the "Host" property (this.Host) is available.

For your code to work as is, change the control block type from <#+ #> to <# #>. This will create your "myFile" variable in the main function and it will have access to the "Host" property.

If you want to understand more about how T4 works you can download my extension, I have a feature (its part of the community version so you can use it for free) that will show you the actually class that is generated and used by the T4 framework, it should help shed some light on what T4 is doing and why this would not work.

这篇关于T4 - 错误60:一个字段初始不能引用非静态字段,方法或属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆