我有创建使用的CodeDOM编译一些代码的应用程序。 我可以看到,所产生的组件是在存储器中。 但是,当我打电话Type.GetType(typeName的),则返回null。 我觉得这有点混乱。
我究竟做错了什么?
static void Main(string[] args)
{
// FYI: Code is some dummy class with only 1 instance method.
string code = System.IO.File.ReadAllText("CodeToCompile.cs.txt");
string errors = null;
Assembly asm = DynamicCompiler.Compile(code, generateInMemory: true, generateDebugInfo: false, message: ref errors);
// Get type from the generated assembly. We know there is only one.
Type oneAndOnlyTypeInAssembly = asm.GetTypes().First();
string typeName = oneAndOnlyTypeInAssembly.AssemblyQualifiedName;
// Tell the type system to return instance of type based on fully qualified name.
// I'd expect this to work, since the assembly is already loaded to memory.
Type sameType = Type.GetType(typeName);
if (sameType != null)
{
Console.WriteLine("Type found and equal={0}", oneAndOnlyTypeInAssembly.Equals(sameType));
}
else
{
Console.WriteLine("Type NOT FOUND");
}
}