.NET实例方法在这里面== NULL - 为什么这可能吗? [英] this == null inside .NET instance method - why is that possible?

查看:90
本文介绍了.NET实例方法在这里面== NULL - 为什么这可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为,它是不可能这样是实例方法体内空。下面这个简单的程序演示,这是可能的。这是一些记录的行为?

I've always thought that it's impossible for this to be null inside instance method body. Following simple program demonstrates that it is possible. Is this some documented behaviour?

class Foo
{
    public void Bar()
    {
        Debug.Assert(this == null);
    }
}

public static void Test()
{            
    var action = (Action)Delegate.CreateDelegate(typeof (Action), null, typeof(Foo).GetMethod("Bar"));
    action();
}



更​​新

UPDATE

我同意回答说这是怎么这种方法记录在案。不过,我真的不明白这种行为。尤其是因为它不是如何C#设计。

I agree with the answers saying that it's how this method is documented. However, I don't really understand this behaviour. Especially because it's not how C# is designed.

我们已经得到了从别人(可能是.NET组
中的一个报告使用C#(还以为是尚未命名C#当时))谁了
编写代码来调用一个空指针的方法,但他们没有
获得一个例外,因为该方法没' ŧ访问任何字段(即
这种为空,但没有在该方法中使用它)。该方法然后
称为另一种方法,它的确使用了这一点,并抛出了一个
例外,有点头刮接踵而至。他们想通后
了,他们给我们送来一记一下吧。
我们认为能够调用的方法对空实例是
有点怪。彼得·金果做了一些测试,看看在PERF影响
总是使用callvirt的是什么,这是足够小,我们决定
进行更改。

We had gotten a report from somebody (likely one of the .NET groups using C# (thought it wasn't yet named C# at that time)) who had written code that called a method on a null pointer, but they didn’t get an exception because the method didn’t access any fields (ie "this" was null, but nothing in the method used it). That method then called another method which did use the this point and threw an exception, and a bit of head-scratching ensued. After they figured it out, they sent us a note about it. We thought that being able to call a method on a null instance was a bit weird. Peter Golde did some testing to see what the perf impact was of always using callvirt, and it was small enough that we decided to make the change.

http://blogs.msdn.com/b/ericgu/archive/2008/07/02/why-does-c-always-use-callvirt.aspx

推荐答案

由于你传递 firstArgument Delegate.CreateDelegate

所以,你调用一个空的对象上的实例方法。

So you're calling an instance method on a null object.

http://msdn.microsoft.com/en-美国/库/ 74x8f551.aspx

如果firstArgument是一个空引用和方法是一个实例方法,
结果取决于委托类型类型和
方法的签名:

If firstArgument is a null reference and method is an instance method, the result depends on the signatures of the delegate type type and of method:

如果类型的签名明确包括隐藏的首
参数法,委托被认为代表了一个开放的
实例方法。当调用委托,在
参数列表中的第一个参数传递给
方法的隐藏的实例参数。

If the signature of type explicitly includes the hidden first parameter of method, the delegate is said to represent an open instance method. When the delegate is invoked, the first argument in the argument list is passed to the hidden instance parameter of method.

如果的签名方法和类型匹配(即所有参数
类型兼容),则委托据说超过
空引用被关闭。调用委托就像是一个空实例,这是不以
一个特别有用的东西做调用一个实例
方法。

If the signatures of method and type match (that is, all parameter types are compatible), then the delegate is said to be closed over a null reference. Invoking the delegate is like calling an instance method on a null instance, which is not a particularly useful thing to do.

这篇关于.NET实例方法在这里面== NULL - 为什么这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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