Wednesday, May 7, 2008

Dynamic assembly generation

Interestingly enough, if you build a dynamic assembly, you can not call GetExportedTypes on it, because the method is not implemented. Very funny, ha ha.


Microsoft has confirmed it is a bug


I encountered this bug while trying to compile boo code with boo compiler


[Test, ExpectedExceptionAttribute(typeof(NotSupportedException))]
public void GetExportedTypes_NotSuportedInDynamicAssembly()
{
string assemblyText = @"
class Foo:
x as int
";
BooAssemblyCompiler compiler = new BooAssemblyCompiler("test", assemblyText);
Type[] types = compiler.CompilerContext.GeneratedAssembly.GetExportedTypes();
}



BooAssemblyCompiler is just a helper class, which inside just does these steps

// creates a boo compiler
m_compiler = new BooCompiler();

// adds a CompileToMemory step to the compiler pipeline
m_compiler.Parameters.Pipeline = new Boo.Lang.Compiler.Pipelines.CompileToMemory();

// defines that an output should be a library
m_compiler.Parameters.OutputType = CompilerOutputType.Library;

// finally, compiles boo text
StringInput input = new StringInput(assemblyName, booText);
m_compiler.Parameters.Input.Add(input);
m_compilerContext = m_compiler.Run();