我在以 string firstLine
开头的最后一行出现错误:
<#+
int Year = (int)(DateTime.UtcNow.Year);
int Month = (int)(DateTime.UtcNow.Month);
int Day = (int)(DateTime.UtcNow.Day);
int RevisionNumber = (int)(DateTime.UtcNow - new DateTime(2013,1,1)).TotalDays;
string multiLine = System.IO.File.ReadAllText("C:\\Users\\kozub.sebastian\\Documents\\Visual Studio 2012\\Projects\\Ris\\RisSystem\\RisSystem\\Properties\\AssemblyInfo.cs");
string firstLine = multiLine.Substring(0, multiLine.IndexOf('\n'));
#>
错误是:
A field initializer cannot reference the non-static field, method, or property
最后两行在我元素的普通 C# 文件中完美运行。
最佳答案
问题出在<#+ #>
.添加+
在 <#
之后说里面的代码是类的一部分,类的字段应该用静态成员、常量等或使用类构造函数初始化。
要将代码视为程序脚本,应将代码插入其中
<# #>
标记。
关于c# - T4 - 字段初始值设定项不能引用非静态字段、方法或属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27962942/