我有一个使用 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");
}
}