[Update on 2008-01-06]
I’ve modified the post to reflect the information Dino provided. It turns out the SpecialName attribute is not required on extension methods but only when you are defining normal extension methods only when defining new operators.
One of my projects implemented in C# makes frequent use of extension methods. Recently I started using IronPython to script that project and what I learned is that consuming those extension methods in C# is straightforward but with IronPython some extra work is involved.
Because it isn’t entirely obvious what one needs to do to make this work, I’ll use this post to elaborate what I learned.
The diagram below summarizes it …
1 - Add a reference to Microsoft.Scripting.Dll In the Visual Studio project file for B, Add a reference to the Microsoft.Scripting.Dll file (this is found in the IronPython 2.0 install folder) 2 – Add an attribute that identifies that B extends A Add this at the top of B’s source file, just after the using statements.
1 - Add a reference to Microsoft.Scripting.Dll
In the Visual Studio project file for B, Add a reference to the Microsoft.Scripting.Dll file (this is found in the IronPython 2.0 install folder)
2 – Add an attribute that identifies that B extends A
Add this at the top of B’s source file, just after the using statements.
In this example
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace DemoIronPythonExtensionMethods { public class ClassToBeExtended { }
public static class DemoExtensionClass { public static string Foo(this string s) { return "Bar"; }
public static string Foo(this ClassToBeExtended c) { return "Bar"; } }
}
[assembly: Microsoft.Scripting.Runtime.ExtensionType( typeof(DemoIronPythonExtensionMethods.ClassToBeExtended), typeof(DemoIronPythonExtensionMethods.DemoExtensionClass) )]
[assembly: Microsoft.Scripting.Runtime.ExtensionType( typeof(string), typeof(DemoIronPythonExtensionMethods.DemoExtensionClass) )]
>>> import clr >>> import System >>> clr.AddReference("DemoIronPythonExtensionMethods.dll") >>> import DemoIronPythonExtensionMethods >>> c1 = DemoIronPythonExtensionMethods.ClassToBeExtended() >>> c1.Foo() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'ClassToBeExtended' object has no attribute 'Foo' >>> s1= "HELLO WORLD" >>> s1.Foo() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'str' object has no attribute 'Foo'
>>> import clr >>> import System >>> clr.AddReference("DemoIronPythonExtensionMethods.dll") >>> import DemoIronPythonExtensionMethods >>> c1 = DemoIronPythonExtensionMethods.ClassToBeExtended() >>> c1.Foo() 'Bar' >>> s1= "HELLO WORLD" >>> s1.Foo() 'Bar'