Using CSharpCodeProvider fails on loading referenced dll

0

I have a C# code that dynamically creates classes and runs them, in the code that is generated I am adding a reference to the current dll (the one that is creating them).

The created classes fail compilation due to a missing dll

        var provider = new Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider();
        CompilerParameters cp = new CompilerParameters();
        cp.GenerateExecutable = false;
        cp.GenerateInMemory = true;
        cp.TreatWarningsAsErrors = false;
        cp.ReferencedAssemblies.Add(Assembly.GetAssembly(typeof(Enumerable)).Location);
        cp.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
        CompilerResults cr = provider.CompileAssemblyFromFile(cp, scripts.ToArray());

The variable cr contains the following errors:

(0, 0) Metadata file 'my dll' could not be opened -- Could not load file or assembly 'System.Collections.Immutable, Version=1.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) (0, 0) Metadata file 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll' could not be opened -- Could not load file or assembly 'System.Collections.Immutable, Version=1.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) (0, 0) Metadata file 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Runtime\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Runtime.dll' could not be opened -- Could not load file or assembly 'System.Collections.Immutable, Version=1.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) (0, 0) Metadata file 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll' could not be opened -- Could not load file or assembly 'System.Collections.Immutable, Version=1.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

gac
csharpcodeprovider
asked on Stack Overflow Apr 3, 2019 by user2342987

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0