Task: create an assembly on the fly with the resource file inside.

I try this option:

private class InProcessCompiler { private string sourceCode = @" using System; namespace Bar { public class Test { } } "; public Assembly BuildAssembyWithResources(string resource) { CompilerParameters cp = new CompilerParameters() { GenerateInMemory = false, OutputAssembly = "test.dll", CoreAssemblyFileName = "test.dll", GenerateExecutable = false, CompilerOptions = "/out:C:\\" }; cp.EmbeddedResources.Add(resource); // Hard-code parameters for test. var po = new Dictionary<string, string> { { "CompilerVersion", "v4.0" } }; var p = new CSharpCodeProvider(po); var ass = p.CompileAssemblyFromSource(cp, sourceCode); return ass.CompiledAssembly; //return Assembly.GetExecutingAssembly(); } } 

But when dealing with the CompiledAssembly property, use the exit: ((System.IO.FileNotFoundException)(ass.CompiledAssembly))._fusionLog

Here is the complete log:

 === Pre-bind state information === LOG: Where-ref bind. Location = D:\...Tests\bin\Debug\test.dll LOG: Appbase = file:///D:/...Tests/bin/Debug/ LOG: Initial PrivatePath = NULL Calling assembly : (Unknown). === LOG: This bind starts in LoadFrom load context. WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load(). LOG: No application configuration file found. LOG: Using host configuration file: LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. LOG: The same bind was seen before, and was failed with hr = 0x80070002. 

Tell me what the problem is or what is missing, please.

Errors property

  • The CodeDomProvider.CompileAssemblyFromSource method returns CompilerResults , which has the Errors property. Look what's in it. - Pavel Mayorov
  • @PavelMayorov, updated the question that I haven’t yet found any coherent information on CS2032 - Anton Komyshan
  • @AntonKomyshan: And if you remove resources, then everything works? - VladD
  • @VladD, yes, the problem was resources. - Anton Komyshan

2 answers 2

Error CS2032 indicates an invalid character in the compiler options. Apparently, swears at this:

 CompilerOptions = "/out:C:\\" 

here should be specified incl. the file itself. But in theory, there is no point in messing with the out key, set the full path like this:

 OutputAssembly = "с:\\test.dll" 

    The problem was solved as follows:

    First I had to write resources to a file, and then pass the file path to the BuildAssembyWithResources method. Apparently EmbeddedResources.Add did not expect the resource string, but the path to the resource. This is how trite ...

    • 2
      Before working with an unfamiliar API, it is always better to first read the documentation :). From MSDN about EmbeddedResources: "A collection that contains the .NET Framework." - andreycha
    • @andreycha, I didn’t want to create an extra file, but yes, it will be a lesson) - Anton Komyshan
    • Mark your answer as correct. - andreycha