c# - 以编程方式为 resx 文件生成 Designer.cs (ResXResourceWriter/ResXResourceReader)

标签 c# resx resgen

我正在使用不生成 .Designer.cs 文件的 ResXResourceWriter/ResXResourceReader 在 TFS 中创建/更新 resx 文件。我看到 Resgen 创建了 .Designer.cs。我如何以编程方式调用它以在某个 TFS 文件路径生成 .Designer.cs?是这样的吗?

ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Program Files\Microsoft.NET\SDK\v2.0 64bit\Bin\ResGen.exe");
                startInfo.WindowStyle = ProcessWindowStyle.Minimized;
                startInfo.Arguments = "ResourceName.resx /publicClass /str:cs, Namespace, ResourceName, ResourceName.Designer.cs";
                Process.Start(startInfo);

最佳答案

我发现了如何以编程方式生成 .Designer.cs 文件。

string[] unmatchedElements;
var codeProvider = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.CodeCompileUnit code =
    System.Resources.Tools.StronglyTypedResourceBuilder.Create(
        "MyClass.resx", "MyClass", "my.namespace", codeProvider, 
        true, out unmatchedElements); // Needs System.Design.dll

using(StreamWriter writer = new StreamWriter("MyClass.Designer.cs", false,
    System.Text.Encoding.UTF8))
{
    codeProvider.GenerateCodeFromCompileUnit(code, writer,
        new System.CodeDom.Compiler.CodeGeneratorOptions());
}

// Returns false if at least one ppty couldn't be generated.
return unmatchedElements.Length == 0; 

关于c# - 以编程方式为 resx 文件生成 Designer.cs (ResXResourceWriter/ResXResourceReader),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3729255/

相关文章:

c# - .NET 资源文件只有在与 MVC View 位于同一文件夹中时才能成功引用

c# - 是否可以在 C# 的 OpenFileDialog 框中锁定顶部和左侧导航?

c# - 在 xml 文件中搜索数据的最佳方式?

C# 文件到字典,但采用单词对

c# - 添加自定义列 resx 文件

asp.net-mvc - 数据注释和资源不能很好地发挥作用

C# - 删除 WPF DataGrid 中的右边缘行边框

c# - 如何在自定义服务器控件 asp 中使用 .resx 和 .resource 文件?

asp.net - 将资源文件编译到库时出现问题

.net - 从哪里获取 resgen.exe 以便与 NANT 一起使用?