Поделиться через


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

  • Anonymous
    October 11, 2007
    PingBack from http://www.artofbam.com/wordpress/?p=7441

  • Anonymous
    October 11, 2007
    The comment has been removed

  • Anonymous
    October 12, 2007
    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 ...

  • Anonymous
    March 10, 2008
    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?