【问题标题】:Error calling function in T4 templateT4 模板中调用函数时出错
【发布时间】: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

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

怎么了?

【问题讨论】:

    标签: c# .net t4


    【解决方案1】:

    这样称呼它:

    Helper.GetMaxVersionInResources();

    【讨论】:

    • 我自己已经找到原因了,它是一个静态方法。感谢您的宝贵时间。
    猜你喜欢
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 2023-03-24
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多