开发者

extension methods from C# dll doesn't work as extensions in VB.NET

actually I don't know whether they开发者_如何转开发 should work

I made a library in C# and I've been told by people that one of mine methods don't work in VB.NET as extension http://valueinjecter.codeplex.com/Thread/View.aspx?ThreadId=227498

this is the method:

public static PropertyDescriptorCollection GetProps(this object o)
{
   return GetProps(o.GetType());
}


In general C# extension methods work just fine in VB.Net and vice versa. The one exception is when the this parameter is explicitly typed to Object. For legacy reasons VB.Net does not support the use of extension methods on references that are typed to Object.

The reason why is has the potential to cause code to silently recompile with different semantics. VB.Net (and C#) took the stance that importing a namespace which contains extension method(s) should not cause a silent rebind of existing code to the extension method. If VB.Net allowed extension methods on Object then it would be possible for late bound calls to silently rebind to the extension method and hence change the code.

For example. Consider the following written before your extension method.

Dim local As Object = ... 
local.GetProps() ' This is a late bound call

If VB.Net allowed the GetProps extension method to be defined on Object then simply importing your namespace would change the meaning of GetProps from late bound to an extension method call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜