Extension methods Interoperability between languages


Extension methods written in C# can be imported and called with Extension method semantics in VB and vice versa. This is possible since me decorate the assemblies , types and methods in the same manner. Using the Attribute

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]

public sealed class ExtensionAttribute : Attribute { } 

This should be a special note to developers writing their own compilers or doing IL gen. This decoration allows the importer to precisely identify the classes to import when looking for extension methods. Allowing it to omit unnecessary types and their dependencies when looking for extension methods, which can quickly addup.

So if your doing anything custom be sure to inspect the IL to see if other languages can discover and call your extension methods seamlessly

Comments (4)

  1. Peter Ritchie says:

    This also means you can write methods in Visual Studio 2005 that will work as extension methods ion Visual Studio 2008.

    See http://msmvps.com/blogs/jon.skeet/archive/2007/08/19/extension-methods-and-net-2-0.aspx for more details.

  2. Sree_C says:

    that is correct … one could define the attribute in framework 2.0 and decorate the methods approreatly. Though calling these with extension semantics might be possible but is not something we enginnered the binding code for …

  3. avantika srivastava says:

    hi

    i am new to the blog. just wanted to know more about extension methods. are extension methods behaviour similar to that of static methods?

Skip to main content