My goals is to generate an assembly using CSharpCodeProvider and then convert it to a byte[] so that it can be stored in a database for later use.
The code I've been trying to use is as follows:
public static Assembly Compile(String sourceCode)
{
var codeProvider = new CSharpCodeProvider();
var parameters = new CompilerParameters();
parameters.CompilerOptions = "/optimize";
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = false;
parameters.IncludeDebugInformation = false;
parameters.OutputAssembly = "my.dll";
parameters.GenerateInMemory = true;
parameters.ReferencedAssemblies.Add("System.dll");
parameters.ReferencedAssemblies.Add("System.Core.dll");
parameters.ReferencedAssemblies.Add("System.Data.dll");
parameters.ReferencedAssemblies.Add("System.Data.DataSetExtensions.dll");
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, sourceCode);
Assembly assembly = null;
if (!results.Errors.HasErrors)
{
assembly = results.CompiledAssembly;
return assembly;
}
throw new Exception("Invalid source code");
}
I've successfully tested the returned assembly using the following:
var assembly = SchemaBuilder.Compile(sourceCode);
var personType = assembly.GetType("app.Person");
object personObject = Activator.CreateInstance(personType);
Assert.IsNotNull(personObject);
The problem is that when I convert it to a byte[] and try to load it afterwards I keep getting an exception as if the assembly is being corrupted during conversion:
var assembly = SchemaBuilder.Compile(sourceCode);
byte[] dllAsArray;
using (MemoryStream stream = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, assembly);
dllAsArray = stream.ToArray();
}
Assembly assemblyFromByteArray = Assembly.Load(dllAsArray);
The exception:
System.BadImageFormatException: Could not load file or assembly '171 bytes loaded from builder.test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format. ---> System.BadImageFormatException: Bad IL format.