2

I'm generating an assembly (*.dll) at runtime. the compilation process is performed using CodeDom, as is recommended in following post:

Generating DLL assembly dynamically at run time

My code and assembly are generated successfully, not errors. The problem comes when I'm attempting load this generated assemblies at runtime via reflection using :

 // load for reflection only
 var _assemblyTempLoad = Assembly.LoadFrom(assembly.FullName);

Following exception is thrown:

"Could not load file or assembly 'nameforassembly.dll' or one of its dependencies. The module was expected to contain an assembly manifest."

How to generate the manifest file or fix this issue?

I want clarify that assembly is generated at runtime, using following code:

CompilerResults compilerResult = codeDomProvider.CompileAssemblyFromFile(compilerParameters, Path.Combine(path, sourceCodeFile));`

Thank you in advance

Community
  • 1
  • 1
César Qüeb
  • 121
  • 7
  • 1
    Is it possible the file is either corrupt or compiled for the wrong framework or compiled for 64-bit and you are trying to load into a 32 bit process? You might get a more accurate error if you try to add the compiled dll into your project (just as a test). – sgmoore Jul 14 '12 at 17:02
  • Give the same you link to, you can specify the platform by adding a line like parameters.CompilerOptions = "/platform:anycpu" ; – sgmoore Jul 17 '12 at 16:45

2 Answers2

0

Have you tried the following

compilerParameters.CompilerOptions = string.Format("/win32manifest: {0}", manifestFilename);

If you've already have the CompilerOptions set to some value, just concatenate the strings

compilerParameters.CompilerOptions += string.Format(" /win32manifest: {0}", manifestFilename);

The win32manifest parameter tells the compiler to also generate a manifest file.

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
0

when an assembly is created at runtime (on the fly), the assembly info or metadata is not placed into assembly automatically. The use of the [Assembly] attribute was necessary too. On this way, the last step in the process was place the /platform argument to the compiler (thanks sgmoore). I can saw this using Redgate reflector. The assembly was shown without versioning and metadata attributes. Like this:

streamWriter.WriteLine(string.Format("[assembly: AssemblyTitle(\"{0}\")]", yourassembly.propertyfornamespace.Replace(" ", "")));
streamWriter.WriteLine(string.Format("[assembly: AssemblyDescription(\"{0}\")]", yourassembly.propertywithdescription));
César Qüeb
  • 121
  • 7