【发布时间】:2017-05-24 14:22:25
【问题描述】:
我有以下 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:\GREDOS\Main\GREDOS.Deployment\GREDOS.Deployment.Resources\SqlVersionFinder.cs
编译转换时出错:当前上下文 GREDOS.Deployment.Resources C:\GREDOS\Main\GREDOS.Deployment\GREDOS.Deployment.Resources\SqlVersionFinder.tt 中不存在名称“GetMaxVersionInResources”
错误编译转换:预期)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(); #> 中方法的调用,一切正常。
怎么了?
【问题讨论】: