为什么这个扩展方法扔在VB.NET一个NullReferenceException? [英] Why does this extension method throw a NullReferenceException in VB.NET?

查看:233
本文介绍了为什么这个扩展方法扔在VB.NET一个NullReferenceException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从previous经验,我一直在IM pression,这是完全合法的(尽管可能不建议)来调用扩展方法上的空实例。因此,在C#中,该code编译和运行:

  // code在静态类
静态布尔ISNULL(此对象OBJ){
    返回的obj == NULL;
}

// $ C $别处Ç
对象X = NULL;
!布尔存在= x.IsNull();
 

不过,我刚刚组建了一个小套房例如code为我的开发团队的其他成员(我们刚刚升级到.NET 3.5和我已经分配得到球队最高速度的任务对一些提供给我们的新功能),我写了我的认为的是VB.NET相当于上述code,却发现它实际上抛出一个的NullReferenceException 。在code我写的是这样的:

 'code模块
<扩展()> _
功能ISNULL(OBJ BYVAL作为对象)作为布尔
    返回obj是没有
端功能

code别处
昏暗exampleObject为对象=无
昏暗的存在由于布尔=不exampleObject.IsNull()
 

调试器停在那里,好像我会叫一个实例方法。我是不是做错了什么(例如,是否有在我定义的C#和VB.NET的扩展方法方式有些微妙的差异)?它实际上是不可以合法的,在VB.NET一个空实例调用扩展方法,虽然它是合法的,在C#中? (我还以为这是相对于一个特定语言的事情。NET的东西,但也许我错了。)

有人能解释这个给我?

解决方案

您不能扩展的对象类型,在VB.NET。

  
    

主要是,我们不允许扩展方法被称为过任何前pression是静态类型的对象。这是必要的,以prevent c您的任何现有的后期绑定$ C $可能是由扩展方法被打破写的。

  

参考:

  • <一个href="http://blogs.msdn.com/vbteam/archive/2007/01/24/extension-methods-and-late-binding-extension-methods-part-4.aspx">http://blogs.msdn.com/vbteam/archive/2007/01/24/extension-methods-and-late-binding-extension-methods-part-4.aspx

  • <一个href="http://johnwest.spaces.live.com/Blog/cns!EBA860160D5F5D75!463.entry">http://johnwest.spaces.live.com/Blog/cns!EBA860160D5F5D75!463.entry

From previous experience I had been under the impression that it's perfectly legal (though perhaps not advisable) to call extension methods on a null instance. So in C#, this code compiles and runs:

// code in static class
static bool IsNull(this object obj) {
    return obj == null;
}

// code elsewhere
object x = null;
bool exists = !x.IsNull();

However, I was just putting together a little suite of example code for the other members of my development team (we just upgraded to .NET 3.5 and I've been assigned the task of getting the team up to speed on some of the new features available to us), and I wrote what I thought was the VB.NET equivalent of the above code, only to discover that it actually throws a NullReferenceException. The code I wrote was this:

' code in module '
<Extension()> _
Function IsNull(ByVal obj As Object) As Boolean
    Return obj Is Nothing
End Function

' code elsewhere '
Dim exampleObject As Object = Nothing
Dim exists As Boolean = Not exampleObject.IsNull()

The debugger stops right there, as if I'd called an instance method. Am I doing something wrong (e.g., is there some subtle difference in the way I defined the extension method between C# and VB.NET)? Is it actually not legal to call an extension method on a null instance in VB.NET, though it's legal in C#? (I would have thought this was a .NET thing as opposed to a language-specific thing, but perhaps I was wrong.)

Can anybody explain this one to me?

解决方案

You cannot extend the object type in VB.NET.

Mainly, we do not allow extension methods to be called off of any expression that is statically typed as "Object". This was necessary to prevent any existing late bound code you may have written from being broken by extension methods.

Reference:

这篇关于为什么这个扩展方法扔在VB.NET一个NullReferenceException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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