gpt4 book ai didi

c# - 自定义 resx 框架 visual studio 2010

转载 作者:行者123 更新时间:2023-11-30 22:24:15 25 4
gpt4 key购买 nike

我喜欢 visual studio 2010 resx 框架,我希望具有相同的功能但具有自定义列。我想到了以下解决方案:

  1. 将 res.xml 定义为唯一文件以从中生成资源类。
  2. 创建我自己的 visual studio 插件,每次保存我的 res.xml 时都会使用 xml 中的字段生成资源类。

例子:

XML:

 <?xml version="1.0" encoding="utf-8"?>
<resources>
<field name="x" loggerLevel="Verbose"/>
<field name="y" loggerLevel="Details"/>
</resources>

翻译成代码:

class Resources{
public readonly Field x = new Field(LoggerLevel.Verbose);
public readonly Field y = new Field(LoggerLevel.Details);
}

问题是我在这里杀人了吗?有没有更简单的解决方案来实现我的目标?

编辑:修复了 xml。

最佳答案

一个简单的 T4 模板可能就足够了,T4 已经内置在 MSVS 中。将您的 XML 添加到项目中(确保它有效,您当前的 XML 示例无效),并将 T4 模板添加到与您的 XML 文件相同的项目目录中。根据需要编辑命名空间名称和 XML 文件名(示例代码使用“T4Example”作为命名空间,“Example.xml”作为输入文件名)。

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

namespace T4Example
{
class Fields
{
<#
string stringsDir = Path.GetDirectoryName(this.Host.TemplateFile);
string reswFile = Path.Combine(stringsDir, @"Example.xml");
var doc = XDocument.Load(reswFile);
var data = doc.Element("xml")
.Element("fields")
.Elements("field")
.Select(i => Tuple.Create(
i.Attribute("name").Value,
i.Attribute("loggerLevel").Value));
foreach(var tuple in data)
{#>
public readonly Field <#=tuple.Item1#> = new Field(LoggerLevel.<#=tuple.Item2#>);
<# }#>
}
}

关于c# - 自定义 resx 框架 visual studio 2010,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12882571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com