问题描述
在使用的typeof
运营商通过TypeBuilder创建的类型,操作员将返回null。
我很好奇为什么发生这种情况以及如何防止它。
我开始认为这是在一个错误VS即时窗口,但我不能肯定。
这很容易,首先指责别人
好吧...代码重现问题:
静态无效的主要()
{
assemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
新的AssemblyName(MyAssembly程序),
AssemblyBuilderAccess .RunAndSave);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(MyModule的);
TypeBuilder typeBuilder = moduleBuilder.DefineType(MyType的TypeAttributes.Public的typeof(ArrayList中));
的ArrayList O =(ArrayList的)Activator.CreateInstance(typeBuilder.CreateType());
Console.WriteLine(o.GetType()名);
}
如果你把一个断点变量后0
键,键入 typeof运算(的MyType)
在VS立即的Windows,你会得到这个问题。
First of all, the claim is correct. If you write this program and then stop it in the debugger and say "typeof(MyType)" in the immediate window, the result that comes back is "null".
Beats the heck out of me. If I had to guess, I'd say that maybe the expression evaluator is communicating with the CLR's debugging subsystem to try and get a metadata token for the type by its name, and the CLR is returning some garbage nil token rather than producing an error.
I hasten to emphasize that this is a guess; I have not actually debugged it.
That seems likely. The correct thing that it should be doing is giving the error "the type or namespace 'MyType' is not valid in this scope". The bug will almost certainly be in the C# runtime expression evaluator, not in the immediate window itself.
Thanks for bringing the issue to my attention. I'll file a bug with the expression evaluator maintainers and we'll see if they can address the issue.
If it hurts when you type "typeof(MyType)" then stop typing that.
这篇关于typeof运算(T)可能返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!