c# - T4 命名空间和类型解析

标签 c# asp.net-mvc resources t4

我正在尝试创建 T4 模板,它将获取我的应用程序全局资源,遍历静态属性并创建一个具有 const string 属性的静态类,这样我就能够获取资源名称作为 string 使用强类型。

这是我现在拥有的:

<#@ template debug="true" hostSpecific="true" #>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ output extension=".cs"#>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#  CodeGenerationTools code = new CodeGenerationTools(this);
    MetadataLoader loader = new MetadataLoader(this);
    CodeRegion region = new CodeRegion(this, 1);
    MetadataTools ef = new MetadataTools(this);
    string namespaceName = code.VsNamespaceSuggestion();
if (!String.IsNullOrEmpty(namespaceName))
{ #>
namespace <#=code.EscapeNamespace(namespaceName)#>
{
<#    PushIndent(CodeRegion.GetIndent(1));
} #>
    public static class ResourceStrings {
<#      var props = typeof(Resources).GetProperties(BindingFlags.Static);
        foreach (var prop in props) { #>
            public const string <#= prop.Name #> = "<#= prop.Name #>";
<#      } #>
    }
<# if (!String.IsNullOrEmpty(namespaceName))
{
    PopIndent(); #>
}
<# } #>
<#+
  // Insert any template procedures here
#>

问题是它无法在 typeof() 中找到 Resources 命名空间。 Resources 命名空间是我的 Localize.designer.cs 文件(Localize.resx 的自动生成文件)的命名空间。

我在这里错过了什么?

最佳答案

我也有类似的想法。我想为我的用户设置提供一个带有 const 字符串值的静态类,以便在 Windows 窗体绑定(bind)中使用强类型而不是字符串:

this.textBox1.DataBindings.Add("Text", Properties.Settings.Default, Properties.Settings.PropertyNames.SomeStringValue);

但是,我发现t4模板无法访问当前项目的类型。 您必须使用

加载文件
<#@ assembly name="$(TargetPath)" #>

但是,如果您这样做并重新生成您的模板,您不会重新生成您的项目,因为该文件正在使用中,除非您关闭 visual studio。

长话短说

我放弃了这种方法,现在我自己读取 app.config 文件来获取我需要的数据。 改为读取 Resources.xml 文件应该很容易。

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Xml.Linq" #>

<#
var xmlFile = Path.Combine(Path.GetDirectoryName(Host.TemplateFile), "..", "app.config");
var query = from x in XElement.Load(xmlFile).Element("userSettings").Element("Your.Namespace.Properties.Settings").Elements("setting")
            select x;
#>

namespace Your.Namespace.Properties
{
    public sealed partial class Settings
    {
        public static class PropertyNames
        {

        <# foreach (var item in query) { #>
        public static readonly string <#=item.Attribute("name").Value#> = @"<#=item.Attribute("name").Value#>";
        <# } #>

        }
    }
}

关于c# - T4 命名空间和类型解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4908922/

相关文章:

c# - .NET (Visual Studio) 在项目之间共享 Assets

c# - 从 View 模型映射到域模型的最佳位置在哪里?

android - 转义 Android 资源 xml 中的单引号

c# - .XML 中包含的 .DLL 注释

c# - 通过字符串和 varchar(max) 之间的比较来更新查询

c# - 使用 linq 查找表中的部分重复项

jQuery AJAX 加载未在 ASP.NET MVC Controller 操作上执行

asp.net-mvc - 如何为最小和最大数量的复选框创建 ASP.NET MVC3 验证器?

android - Android Studio编译错误,显示 “Content is not allowed in prolog”

image - 将位图从资源文件加载到图像