在T4模板

时间:2017-05-24 14:22:25

标签: c# .net t4

我有以下T4模板:

<#@ template debug="true" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Reflection" #>
<#@ output extension=".cs" #>



using System;
using System.Linq;
using System.Reflection;

namespace GREDOS.Deployment.Resources
{
    public static class Constants
    {
        public static readonly Version SupportedDatabaseVersion = new Version("<#= GetMaxVersionInResources(); #>");
    }
}

<#+
public static class Helper
    {
        public static string GetMaxVersionInResources()
        {
            var executingAssembly = Assembly.GetExecutingAssembly();
            var folderName = $"{executingAssembly.GetName().Name}.Sql";
            var versions = executingAssembly.GetManifestResourceNames()
                .Where(f => f.StartsWith(folderName))
                .Select(f => new Version(f.Substring(f.IndexOf("__", StringComparison.Ordinal) + 2,
                        f.LastIndexOf("__", StringComparison.Ordinal) - f.IndexOf("__", StringComparison.Ordinal))
                    .Replace("_", string.Empty)))
                .Distinct();
            return versions.Max(v => v).ToString();
        }
    }
#>

当我保存时,我收到以下错误:

  

错误CS0116命名空间不直接包含字段或方法等成员。 GREDOS.Deployment.Resources C:\ GREDOS \ Main \ GREDOS.Deployment \ GREDOS.Deployment.Resources \ SqlVersionFinder.cs 1

     

错误CS0103名称&#39; ErrorGeneratingOutput&#39;在当前上下文中不存在。 GREDOS.Deployment.Resources C:\ GREDOS \ Main \ GREDOS.Deployment \ GREDOS.Deployment.Resources \ SqlVersionFinder.cs

     

错误编译转换:名称&#39; GetMaxVersionInResources&#39;在当前上下文中不存在GREDOS.Deployment.Resources C:\ GREDOS \ Main \ GREDOS.Deployment \ GREDOS.Deployment.Resources \ SqlVersionFinder.tt

     

编译转换时出错:预期)GREDOS.Deployment.Resources C:\ GREDOS \ Main \ GREDOS.Deployment \ GREDOS.Deployment.Resources \ SqlVersionFinder.tt

     

错误编译转换:预期} GREDOS.Deployment.Resources C:\ GREDOS \ Main \ GREDOS.Deployment \ GREDOS.Deployment.Resources \ SqlVersionFinder.tt

如果我删除对<#= GetMaxVersionInResources(); #>中方法的调用,一切正常。

出了什么问题?

1 个答案:

答案 0 :(得分:1)

这样称呼:

Helper.GetMaxVersionInResources();