CodeDom 컴파일러를 사용하여 코드를 만드는 앱이 있습니다. 생성 된 어셈블리가 메모리에 있음을 알 수 있습니다. 하지만 Type.GetType (typeName)을 호출하면 null을 반환합니다. 나는 이것을 조금 혼란스럽게 느낀다.Type.GetType (string)은 동적으로 생성되는 유형을 인식해야합니까?
내가 뭘 잘못하고 있니?
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");
}
}
DynamicCompiler.Compile은 CodeDom의 CSharpCodeProvider.CompileAssemblyFromSource를 래핑하는 단순한 도우미 메서드입니다. 나는 간결함을 위해 그것을 버렸다. –