Trying to compile this sample of code:
var c = new CSharpCodeProvider();
var cp = new CompilerParameters();
var className = $\"CodeEvaler_{Guid.NewGuid().ToStrin
In my case I was migrating a project from .NET Framework 4.7.2
to .NET Standard 2.0
. In this project I have code to generate an assembly during runtime by using CSharpCodeProvider
. Could compile the project but then during runtime my code threw an exception when generating an assembly from Dom. I got this error:
CS0012: The type 'System.Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ffffd51'.
As a consequence the assembly was not created during runtime.
In order to fix that I have added .netstandard
for the compiler as a referenced assembly like that:
var compilerParameters = new CompilerParameters();
var netstandard = Assembly.Load("netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ffffd51");
compilerParameters.ReferencedAssemblies.Add(netstandard.Location);
That did the trick for me.