简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM