简体   繁体   English

PublicResXFileCodeGenerator的T4模板

[英]T4 template for PublicResXFileCodeGenerator

I want to do the following: add a switch in my Web.config and whenever it is turned on, any value coming from a .resx file will appear together with the name of the .resx file it came from. 我想要执行以下操作:在我的Web.config中添加一个开关,每当打开它时,来自.resx文件的任何值都将与它来自的.resx文件的名称一起出现。 This is mainly for debugging purposes. 这主要用于调试目的。

My current understanding is that the way to do this is create a custom T4 template, and use it instead of the PublicResXFileCodeGenerator . 我目前的理解是,这样做的方法是创建一个自定义T4模板,并使用它而不是PublicResXFileCodeGenerator

I am trying to find a template that does what the PublicResXFileCodeGenerator does, and then build upon that. 我试图找到一个模板来执行PublicResXFileCodeGenerator所做的事情,然后在此基础上构建。 Is there such a template somewhere in my installation of Visual Studio? 我的Visual Studio安装中是否有这样的模板? If not, can I find such a template from an official source online? 如果没有,我可以在线找到官方来源的模板吗?

Visual Studio uses the StronglyTypedResourceBuilder Class to transform .resx files into "hard coded" resources. Visual Studio使用StronglyTypedResourceBuilder类将.resx文件转换为“硬编码”资源。 That's why I don't think there is a similar T4 template somewhere in the Visual Studio installation. 这就是为什么我认为在Visual Studio安装中某处没有类似的T4模板。

You might want to create your own T4 template. 您可能想要创建自己的T4模板。 I have no ready-to-use template for your case but this is how I would begin and replace the default resource builder for string resources: 我没有针对您的案例的现成模板,但这是我将如何开始并替换字符串资源的默认资源构建器:

  1. Remove "ResXFileCodeGenerator" from the CustomTool-Property in the resource file's Property Grid. 从资源文件的Property Grid中的CustomTool-Property中删除“ResXFileCodeGenerator”。
  2. Delete the existing .Designer.cs file 删除现有的.Designer.cs文件
  3. Add a setting "DebugResources" to the web.config 将一个“DebugResources”设置添加到web.config
  4. Open the .resx file inside your T4 template as a XDocument (.resx is an xml format) 打开T4模板中的.resx文件作为XDocument(.resx是xml格式)

     <#@ assembly name="System.Xml" #> <#@ assembly name="System.Xml.Linq #> <#@ import namespace="System.Xml.Linq #> <# string inputFile = System.IO.Path.GetDirectoryName(this.Host.TemplateFile) + @"\\MyResourceFile.resx"; XDocument resDoc = XDocument.Load(inputFile); #> 
  5. Iterate all elements in the .resx file and generate properties for the resources. 迭代.resx文件中的所有元素并生成资源的属性。 Depending on the setting in the web.config add the resource file name to the return value. 根据web.config中的设置,将资源文件名添加到返回值。

     <# foreach(var dataElement in resDoc.Descendants("data") .Where(elem => elem.Attribute("type") == null) { #> public static string <#= dataElement.Attribute("name").Value #> { get { return ResourceManager.GetString("<#= dataElement.Attribute("name").Value #>") + ((bool.Parse(ConfigurationManager.AppSettings["DebugResources"])) ? " (MyResourceFile.resx)" : String.Empty); <# } #> 
  6. Generate the rest of a resource class: the resource manager (you can copy that from the original Designer.cs file) and check how to deal with Images and other resource types. 生成资源类的其余部分:资源管理器(您可以从原始Designer.cs文件中复制该资源管理器)并检查如何处理图像和其他资源类型。

Hope this idea helps. 希望这个想法有所帮助 Maybe I can come up with a complete and reusable .tt sometime. 也许我可以在某个时候想出一个完整且可重复使用的.tt。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM