簡體   English   中英

C#/.NET - CodeDom.Compiler 設置程序集信息屬性

[英]C# / .NET - CodeDom.Compiler Set Assembly Info Attributes

我是 C# 新手,正在嘗試使用 .NET 的 CodeDom.Compiler 來編譯應用程序並在輸出的 exe 中正確生成程序集信息。 我一直在查看 MS Docs / Class 參考資料來這樣做。 是否可以在編譯期間設置程序集信息?

編譯我的源代碼的代碼:

CompilerParameters CParams = new CompilerParameters();
CParams.GenerateExecutable = true;
CParams.OutputAssembly = Output;

string options = "/optimize+ /platform:x86 /target:winexe /unsafe";
if (Icon != null)
    options += " /win32icon:\"" + Icon + "\"";

CParams.CompilerOptions = options;
CParams.TreatWarningsAsErrors = false;
CParams.ReferencedAssemblies.Add("System.dll");
CParams.ReferencedAssemblies.Add("System.Windows.Forms.dll");
CParams.ReferencedAssemblies.Add("System.Drawing.dll");
CParams.ReferencedAssemblies.Add("System.Data.dll");
CParams.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll");

Dictionary<string, string> ProviderOptions = new Dictionary<string, string>();
ProviderOptions.Add("CompilerVersion", "v2.0");

CompilerResults Results = new CSharpCodeProvider(ProviderOptions).CompileAssemblyFromSource(CParams, source);

輸出Exe的匯編信息:

在此處輸入圖片說明

當我實際使用 Writer 保存 exe 時,此功能是否可用?

Writer.WriteResource(FSave.FileName, EncryptedBytes);

我非常感謝任何線索,謝謝。

取而代之的是你的最后一行

        ...
        var unit = new CodeCompileUnit();
        var attr = new CodeTypeReference(typeof(AssemblyVersionAttribute));
        var decl = new CodeAttributeDeclaration(attr, new CodeAttributeArgument(new CodePrimitiveExpression("1.0.2.42")));
        unit.AssemblyCustomAttributes.Add(decl);
        var prov = new CSharpCodeProvider(ProviderOptions);
        var assemblyInfo = new StringWriter();
        prov.GenerateCodeFromCompileUnit(unit, assemblyInfo, new CodeGeneratorOptions());

        var result = prov.CompileAssemblyFromSource(CParams, new[] {"public class p{public static void Main(){}}", assemblyInfo.ToString()});

生成 assemblyinfo 並將其添加到源進行編譯

msdn

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM