Programmatically generate Designer.cs for resx file (ResXResourceWriter/ResXResourceReader)

浪子不回头ぞ 提交于 2019-12-18 12:22:24

问题


I'm creating/updating resx files in TFS using ResXResourceWriter/ResXResourceReader which doesnt generate the .Designer.cs file. I saw that Resgen creates the .Designer.cs. How can i call that programmatically to generate the .Designer.cs at a certain TFS file path? Is it something like this?

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);

回答1:


I found out how to programmatically generate the .Designer.cs file.

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; 


来源:https://stackoverflow.com/questions/3729255/programmatically-generate-designer-cs-for-resx-file-resxresourcewriter-resxreso

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!