gpt4 book ai didi

c# - T4 模板 typeOf(List<>) 导致错误但在 C# 类中有效

转载 作者:行者123 更新时间:2023-11-30 17:04:07 26 4
gpt4 key购买 nike

您好,我似乎在 T4 模板上遇到了一些问题。这是我的代码:

  private void ResolveComplexType(PropertyInfo propertyInfo)
{
var property = propertyInfo.PropertyType;

if (property.IsGenericType && property.GetGenericTypeDefinition() == typeof(List<>))
{
var argumentType = propertyInfo.PropertyType.GetGenericArguments()[0];
PrintPropertiesInfo(argumentType);
}
else
{
PrintPropertiesInfo(property);
}
}

我在 typeof(List<>)) 上收到 Identifier_Literal 预期错误.我在 C# 类上测试了完全相同的方法,它运行良好。

有人知道问题出在哪里吗?

编辑

这是错误:

Error 2 Identifier_Literal expected

最佳答案

使用此示例代码,我在 T4 生成和编译中没有发现任何错误。尝试与您的代码进行比较。

<#@ template inherits="Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation" language="C#v3.5" debug="true" hostSpecific="true" #>
<#@ output extension=".cs" #>
<#@ Assembly Name="System.dll" #>
<#@ Assembly Name="System.Core.dll" #>
<#@ Assembly Name="System.Windows.Forms.dll" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#
// insert your template code here the tempalte code will be syntaxhighlighted
// and you will have intellisense for all namespaces in the full edition
string Greeting = "Hello";
#>
// This is the output code from your template
// you only get syntax-highlighting here - not intellisense
namespace MyNameSpace{
using System.Collections.Generic;
class MyGeneratedClass{
static void main (string[] args){
if(typeof(List<>).ToString() == "yadayadayada" ){System.Console.WriteLine("isYadayadayada");}
System.Console.WriteLine("<#= typeof(List<>) #>");
}
}
}

<#+
// Insert any template procedures here
void foo(){
System.Console.WriteLine(typeof(List<>));
}
#>

关于c# - T4 模板 typeOf(List<>) 导致错误但在 C# 类中有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17942656/

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