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