2017-05-24 137 views
1

我有以下的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名稱'ErrorGeneratingOutput'在當前上下文中不存在。 GREDOS.Deployment.Resources C:\格雷多斯\ MAIN \ GREDOS.Deployment \ GREDOS.Deployment.Resources \ SqlVersionFinder.cs

錯誤編譯變換:名稱 'GetMaxVersionInResources' 並不在當前的背景下存在GREDOS.Deployment.ResourcesÇ :\ GREDOS \ Main \ GREDOS.Deployment \ GREDOS.Deployment.Resources \ SqlVersionFinder.tt

錯誤編譯轉換:預期)GREDOS.Deployment.Resources C:\ GREDOS \ Main \ GREDOS.Deployment \ GREDOS.Deployment.Resources \ SqlVersionFinder.tt

錯誤編譯transform:Expected} GREDOS.Deployment.Resources C:\ GREDOS \ Main \ GREDOS.Deployment \ GREDOS.Deployment.Resources \ SqlVersionFinder.tt

如果我刪除對<#= GetMaxVersionInResources(); #>中的方法的調用一切正常。

怎麼了?

回答

1

這樣稱呼它:

Helper.GetMaxVersionInResources();

+0

我已經找到了自己的理由,這是一個靜態方法。謝謝你的時間。 – Oscar