【问题标题】:How to set assembly version, culture and public key token while compiling with Roslyn?使用 Roslyn 编译时如何设置程序集版本、文化和公钥令牌?
【发布时间】:2015-06-22 20:19:02
【问题描述】:

我正在使用 Roslyn 将 Visual Studio 中的 CSharpCompilation 对象发送到文件。生成的 DLL 不包含除程序集元数据以外的任何程序集信息,如果可能,我想添加版本并对其进行签名。这些如何用 Roslyn 完成?

【问题讨论】:

  • 通过在源代码中添加属性,就像一个普通的 C# 项目。

标签: c# .net compilation roslyn assembly-signing


【解决方案1】:

您需要包含设置 Assembly* 属性的源代码,就像在 VS C# 项目模板中一样。如果你已经这样做了,.NET 版本信息就设置好了。您可以使用反射或 ILSpy 等工具读取该信息。

这样,Explorer 就不会在其属性页中显示任何版本信息。 Explorer 仅显示 Win32 VersionInfo 而不是 .NET 版本信息。您需要使用 Rosyln 发出 Win32 资源代码来设置这些值。幸运的是,有一种方法可以从 .NET 中自动生成 Win32 信息:CreateDefaultWin32Resources

这是一个完整且有效的代码示例:

public void VersionInfoExample()
{
    // 1. Generate AssemblyInfo.cs-like C# code and parse syntax tree
    StringBuilder asmInfo = new StringBuilder();

    asmInfo.AppendLine("using System.Reflection;");
    asmInfo.AppendLine("[assembly: AssemblyTitle(\"Test\")]");
    asmInfo.AppendLine("[assembly: AssemblyVersion(\"1.1.0\")]");
    asmInfo.AppendLine("[assembly: AssemblyFileVersion(\"1.1.0\")]");
    // Product Info
    asmInfo.AppendLine("[assembly: AssemblyProduct(\"Foo\")]");
    asmInfo.AppendLine("[assembly: AssemblyInformationalVersion(\"1.3.3.7\")]");

    var syntaxTree = CSharpSyntaxTree.ParseText(asmInfo.ToString(), encoding: Encoding.Default);

    // 2. Create compilation
    string mscorlibPath = typeof(object).Assembly.Location;
    MetadataReference mscorlib = MetadataReference.CreateFromFile(mscorlibPath, new MetadataReferenceProperties(MetadataImageKind.Assembly));
    CSharpCompilationOptions options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);

    CSharpCompilation compilation = CSharpCompilation.Create("Test.dll",
                            references: new[] { mscorlib },
                            syntaxTrees: new[] { syntaxTree },
                            options: options);

    // 3. Emit code including win32 version info
    using (MemoryStream dllStream = new MemoryStream())
    using (MemoryStream pdbStream = new MemoryStream())
    using (Stream win32resStream = compilation.CreateDefaultWin32Resources(
                                                                versionResource: true, // Important!
                                                                noManifest: false,
                                                                manifestContents: null,
                                                                iconInIcoFormat: null))
    {
        EmitResult result = compilation.Emit(
                                     peStream: dllStream,
                                    pdbStream: pdbStream,
                                    win32Resources: win32resStream);

        System.IO.File.WriteAllBytes("Test.dll", dllStream.ToArray());
    }
}

【讨论】:

  • 这段代码还能用吗?我遵循了您的代码(添加了 win32res 流),在资源管理器中我仍然看到 0.0.0.0。
  • 好的,如果我像你一样做的话,它会起作用。如果您使用 FluentApi (.AddReference(...) ) 它不起作用
猜你喜欢
  • 1970-01-01
  • 2021-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-13
  • 1970-01-01
  • 2020-10-12
相关资源
最近更新 更多