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

标签 c# t4

您好,我似乎在 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/

相关文章:

c# - JSON反序列化对象不起作用

c# - 使用 t4 模板在运行时生成内存中的 SQL

c# - 如何让代码生成遵循我的格式设置?

C# 使用 T4 生成枚举

c# - 如何表示 Span<T> 列表/数组

c# - 在 WPF-Desktop 程序中设置每像素位数

c# - net core identity 2.1 中方法 VerifyTwoFactorTokenAsync() 的问题

c# - 仅使用代码在 IIS 外部使用 CaSTLe-Windsor 托管 WCF 服务

.net - T4 文本模板 - 是否可以从主机获取编译符号?

nuget - 在 T4 模板中加载 NuGet 程序集