使用Roslyn进行编译时如何设置程序集版本,区域性和公共密钥令牌? [英] How to set assembly version, culture and public key token while compiling with Roslyn?

查看:54
本文介绍了使用Roslyn进行编译时如何设置程序集版本,区域性和公共密钥令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Roslyn将Visual Studio中的CSharpCompilation对象发射到文件.生成的DLL除了程序集元数据外,不包含任何程序集信息,如果可能的话,我想添加该版本并对其进行签名.罗斯林怎么办?

I'm using Roslyn to emit a CSharpCompilation object in Visual Studio to a file. The DLL that is generated does not contain any assembly info other than the assembly metadata, and I'd like to add the version and sign it if possible. How can these be done with Roslyn?

推荐答案

您需要像VS C#项目模板一样,包含用于设置Assembly *属性的源代码.如果这样做,将设置.NET版本信息.您可以使用Reflection或ILSpy之类的工具阅读该信息.

You need to include source code which sets the Assembly* attributes just like in the VS C# project templates. If you have done that, the .NET version info is set. You can read that information with Reflection or tools like ILSpy.

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

That way Explorer won't show any version info in its property page. Explorer is only showing Win32 VersionInfo not .NET version info. You need to emit Win32 resource code with Rosyln to set these values. Luckily there's a method to auto generate the Win32 info from the .NET ones: CreateDefaultWin32Resources.

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

Here's a complete and working code sample:

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

这篇关于使用Roslyn进行编译时如何设置程序集版本,区域性和公共密钥令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆