1

I'm trying to emit a dll using Roslyn CSharpCompilation on NET 6 with the assembly info (AssemblyInfo.cs) but it's not working. I'm not getting any error but the binary file doesn't contains any property inside.

Complete Single file example

(copy to console single file app and add Microsoft.CodeAnalysis.CSharp.Scripting reference to go)

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

var assemblyName = "test";
var assemblyVersion = "1.2.3";
var dotnetPath = Path.GetDirectoryName(typeof(object).Assembly.Location) + Path.DirectorySeparatorChar;

var trees = new List<SyntaxTree>();

// AssemblyInfo
var info = Manager.GenerateAssemblyInfo(assemblyName, assemblyVersion);
trees.Add(CSharpSyntaxTree.ParseText(info));

Manager.Compile(trees, dotnetPath, assemblyName);

public static class Manager
{
    // AssemblyInfo
    public static string GenerateAssemblyInfo(string assemblyName, string assemblyVersion)
    {
        var code = @$"using System.Reflection;

[assembly: AssemblyProduct(""{assemblyName}"")]
[assembly: AssemblyCompany(""Test Company"")]
[assembly: AssemblyCopyright(""Copyright © {assemblyName} 2022"")]
[assembly: AssemblyVersion(""{assemblyVersion}"")]";

        return code;
    }

    // Compiler
    public static bool Compile(List<SyntaxTree> trees, string dotnetPath, string assemblyName)
    {
        List<MetadataReference> References = new();

        References.Add(MetadataReference.CreateFromFile(dotnetPath + "System.Private.CoreLib.dll"));
        References.Add(MetadataReference.CreateFromFile(dotnetPath + "System.Runtime.dll"));
        References.Add(MetadataReference.CreateFromFile(dotnetPath + "System.Console.dll"));

        var settings = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
            .WithOptimizationLevel(OptimizationLevel.Release);

        CSharpCompilation compilation = CSharpCompilation.Create(
            assemblyName,
            trees,
            References,
            settings);

        var result = compilation.Emit(
            $"{assemblyName}.dll",
            $"{assemblyName}.pdb");

        if (!result.Success)
        {
            var a = result.Diagnostics;
        }

        return result.Success;
    }
}

And all empty

enter image description here

Santiago
  • 2,190
  • 10
  • 30
  • 59

1 Answers1

-1

I have the exact same problem. I'm generating an assembly at runtime using CSharpCompilation.Emit(...) and in ildasm the Company Name, Copyright, Product Name, ... are all correctly filled in, but the file properties in windows explorer are all empty.

I'm migrating my implementation from CodeDomProvider to CSharpCompilation and when I compare the old assembly (generated with CodeDomProvider) to the new one (generated with CSharpCompilation), they are pretty much identical in ildasm (only different guids). However, the old assembly has correct file properties in windows explorer, while the new assembly has empty file properties in windows explorer. I have no idea why this isn't working ...

After lots of googling, I came across this topic (How to set assembly version, culture and public key token while compiling with Roslyn?) and that works for me!