gpt4 book ai didi

visual-studio-2013 - T4报告编译转换:类,结构中的无效 token 'this'

转载 作者:行者123 更新时间:2023-12-03 14:30:42 24 4
gpt4 key购买 nike

尝试为Immutable Object Graph运行T4模板会产生以下错误

╔═══════╦═══╦══════════════════════════════════════════════════════════════════════════════════════════════════╦═════════════════════════════════════════════════════════╦═══╦════╦══════╗
║ Error ║ 5 ║ Compiling transformation: Invalid token 'this' in class, struct, or interface member declaration ║ c:\dev\ImmutableObjectGraph-master\2013\Demo\Message.tt ║ 1 ║ 1 ║ Demo ║
║ Error ║ 6 ║ Compiling transformation: Method must have a return type ║ c:\dev\ImmutableObjectGraph-master\2013\Demo\Message.tt ║ 1 ║ 6 ║ Demo ║
║ Error ║ 7 ║ Compiling transformation: Type expected ║ c:\dev\ImmutableObjectGraph-master\2013\Demo\Message.tt ║ 1 ║ 12 ║ Demo ║
╚═══════╩═══╩══════════════════════════════════════════════════════════════════════════════════════════════════╩═════════════════════════════════════════════════════════╩═══╩════╩══════╝

报告的行始终是第1行,而完整的t4模板集有数百行。如何解决并解决此问题?

最佳答案

脚本后,T4模板中不能包含文字。

更改

<#@ template debug="true" language="C#" #>
<#+
// scriptlet
#>
<-- empty line here


<#@ template debug="true" language="C#" #>
<#+
// scriptlet
#>

调试

通过使用自定义模板主机调用 PreProcessTemplate ,可以看到T4引擎正在生成C#。

为此,我修改了 Custom Template Host sample:

using Microsoft.VisualStudio.TextTemplating;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace CustomHost
{
class CustomCmdLineHost : ITextTemplatingEngineHost
{
public string TemplateFile { get; private set; }
public string FileExtension { get; private set; }
public Encoding FileEncoding { get; private set; }
public CompilerErrorCollection Errors { get; private set; }

public IList<string> StandardAssemblyReferences { get { return new[] { typeof(System.Uri).Assembly.Location }; } }
public IList<string> StandardImports { get { return new string[] { "System" }; } }

public CustomCmdLineHost(string file)
{
this.TemplateFile = file;
this.FileEncoding = Encoding.UTF8;
this.FileExtension = ".txt";
}

public bool LoadIncludeText(string requestFileName, out string content, out string location)
{
content = location = String.Empty;

if (File.Exists(requestFileName))
{
content = File.ReadAllText(requestFileName);
return true;
}

return false;
}

public object GetHostOption(string optionName)
{
object returnObject;
switch (optionName)
{
case "CacheAssemblies":
returnObject = true;
break;
default:
returnObject = null;
break;
}
return returnObject;
}

public string ResolveAssemblyReference(string assemblyReference)
{
return ResolvePath(assemblyReference);
}

public Type ResolveDirectiveProcessor(string processorName)
{
throw new Exception("Directive Processor not found");
}

public string ResolvePath(string fileName)
{
if (File.Exists(fileName))
{
return fileName;
}

string candidate = Path.Combine(Path.GetDirectoryName(this.TemplateFile), fileName);
if (File.Exists(candidate))
{
return candidate;
}

return fileName;
}

public string ResolveParameterValue(string directiveId, string processorName, string parameterName)
{
return String.Empty;
}

public void SetFileExtension(string extension)
{
FileExtension = extension;
}

public void SetOutputEncoding(System.Text.Encoding encoding, bool fromOutputDirective)
{
FileEncoding = encoding;
}

public void LogErrors(CompilerErrorCollection errors)
{
Errors = errors;
}

public AppDomain ProvideTemplatingAppDomain(string content)
{
return AppDomain.CreateDomain("Generation App Domain");
}
}

class Program
{
static void Main(string[] args)
{
var templateFileName = args[0];

CustomCmdLineHost host = new CustomCmdLineHost(templateFileName);
Engine engine = new Engine();

string language;
string[] refs;
var output = engine.PreprocessTemplate(
// input file
File.ReadAllText(templateFileName), host,
"testClass", "testNamespace", out language, out refs);

string outputFileName = Path.Combine(
Path.GetDirectoryName(templateFileName),
templateFileName + ".generator.cs");

File.WriteAllText(outputFileName, output, host.FileEncoding);

foreach (CompilerError error in host.Errors)
Console.WriteLine(error.ToString());

Console.ReadLine();
}
}
}

检查从模板生成的转换器,在 TransformText()方法之外显示了类似以下的行。看来,源模板中 scriptlet( <#+ #>)之后的所有文字都将以固定方式放入生成的生成器类中。
        #line 1 "C:\dev\ImmutableObjectGraph-master\2013\Demo\Fruit.tt"
this.Write("\n");

删除每个模板文件末尾的换行符可解决此问题。

关于visual-studio-2013 - T4报告编译转换:类,结构中的无效 token 'this',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23281434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com