在 T4 模板中调用函数时出错
Error calling function in T4 template
我有以下 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();
}
}
#>
保存时出现以下错误:
Error CS0116 A namespace does not directly contain members such as fields or methods. GREDOS.Deployment.Resources C:\GREDOS\Main\GREDOS.Deployment\GREDOS.Deployment.Resources\SqlVersionFinder.cs 1
Error CS0103 Name 'ErrorGeneratingOutput' does not exist in the current context. GREDOS.Deployment.Resources C:\GREDOS\Main\GREDOS.Deployment\GREDOS.Deployment.Resources\SqlVersionFinder.cs
Error Compiling transform: Name 'GetMaxVersionInResources' does not exist in the current context GREDOS.Deployment.Resources C:\GREDOS\Main\GREDOS.Deployment\GREDOS.Deployment.Resources\SqlVersionFinder.tt
Error Compiling transform: Expected ) GREDOS.Deployment.Resources C:\GREDOS\Main\GREDOS.Deployment\GREDOS.Deployment.Resources\SqlVersionFinder.tt
Error Compiling transform: Expected } GREDOS.Deployment.Resources C:\GREDOS\Main\GREDOS.Deployment\GREDOS.Deployment.Resources\SqlVersionFinder.tt
如果我删除对 <#= GetMaxVersionInResources(); #>
中方法的调用,一切正常。
怎么了?
这样称呼它:
Helper.GetMaxVersionInResources();
我有以下 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();
}
}
#>
保存时出现以下错误:
Error CS0116 A namespace does not directly contain members such as fields or methods. GREDOS.Deployment.Resources C:\GREDOS\Main\GREDOS.Deployment\GREDOS.Deployment.Resources\SqlVersionFinder.cs 1
Error CS0103 Name 'ErrorGeneratingOutput' does not exist in the current context. GREDOS.Deployment.Resources C:\GREDOS\Main\GREDOS.Deployment\GREDOS.Deployment.Resources\SqlVersionFinder.cs
Error Compiling transform: Name 'GetMaxVersionInResources' does not exist in the current context GREDOS.Deployment.Resources C:\GREDOS\Main\GREDOS.Deployment\GREDOS.Deployment.Resources\SqlVersionFinder.tt
Error Compiling transform: Expected ) GREDOS.Deployment.Resources C:\GREDOS\Main\GREDOS.Deployment\GREDOS.Deployment.Resources\SqlVersionFinder.tt
Error Compiling transform: Expected } GREDOS.Deployment.Resources C:\GREDOS\Main\GREDOS.Deployment\GREDOS.Deployment.Resources\SqlVersionFinder.tt
如果我删除对 <#= GetMaxVersionInResources(); #>
中方法的调用,一切正常。
怎么了?
这样称呼它:
Helper.GetMaxVersionInResources();