typeof运算(T)可能返回null [英] typeof(T) may return null

查看:393
本文介绍了typeof运算(T)可能返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用的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,你会得到这个问题。


解决方案

在使用通过TypeBuilder创建的类型typeof运算符,操作员将返回null。




首先,索赔是正确的。如果你写这个程序,然后停止它在调试器,并说typeof运算(的MyType)中的即时窗口,该回来的结果是空。




我很好奇,为什么发生这种情况。




击败赫克了我。如果我猜,我会说,也许表达式计算器与CLR的调试子系统沟通,试图获得其名称类型元数据标记,和CLR正在恢复一些垃圾零标记,而不是生成错误。



我赶紧强调,这是一种猜测;我还没有实际调试它。




我开始认为这是在直接窗口VS错误




这似乎可能。正确的做法,应该做的是给错误的类型或命名空间的MyType'不在此范围内有效的。该漏洞几乎肯定会在C#运行时表达式求值,而不是在即时窗口本身。



感谢您使问题我的注意。我将文件,用表达式求值维护者的错误,我们将看看他们是否可以解决这个问题。




如何预防呢?




如果当你键入typeof运算(的MyType),然后停止键入它伤害。


When using the typeof operator on type created through TypeBuilder, the operator will return null.

I'm curious why this happens and how to prevent it.


I'm starting to think this is a VS bug in the immediate window, but I'm not quite sure. It's very easy to blame others first.

Ok... code to reproduce issue:

    static void Main()
    {
        AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
            new 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().Name);
    }

If you put a breakpoint after the variable o and type typeof(MyType) in the VS Immediate Windows you'll get the issue.

解决方案

When using the typeof operator on type created through TypeBuilder, the operator will return null.

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".

I'm curious why this happens

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.

I'm starting to think this is a VS bug in the immediate window

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.

how to prevent it?

If it hurts when you type "typeof(MyType)" then stop typing that.

这篇关于typeof运算(T)可能返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆