<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:Wingdings;
        panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
        {font-family:Wingdings;
        panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.MsoPlainText, li.MsoPlainText, div.MsoPlainText
        {mso-style-priority:99;
        mso-style-link:"Plain Text Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:Consolas;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
        {mso-style-priority:99;
        mso-style-link:"Balloon Text Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:8.0pt;
        font-family:"Tahoma","sans-serif";}
span.PlainTextChar
        {mso-style-name:"Plain Text Char";
        mso-style-priority:99;
        mso-style-link:"Plain Text";
        font-family:Consolas;}
span.BalloonTextChar
        {mso-style-name:"Balloon Text Char";
        mso-style-priority:99;
        mso-style-link:"Balloon Text";
        font-family:"Tahoma","sans-serif";}
span.EmailStyle21
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 102.55pt 1.0in 102.55pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='color:#1F497D'>By the way, I fixed this by just overloading the “+” operator the normal way C# operators are overloaded, had it accept “object” on the right-hand-side so Python functions would make their way into it, and then used ObjectOperations.ConvertTo to convert the Python function to a System.EventHandler.<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><div style='border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt'><div><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> Jimmy Schementi <br><b>Sent:</b> Tuesday, February 16, 2010 12:09 AM<br><b>To:</b> Discussion of IronPython<br><b>Subject:</b> RE: Monkey-patching CLR types<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><p class=MsoPlainText><span style='font-size:11.0pt;font-family:"Calibri","sans-serif"'>Dino Viehland wrote:<o:p></o:p></span></p><p class=MsoPlainText><span style='font-size:11.0pt;font-family:"Calibri","sans-serif"'>> If you strongly type handler to a delegate type IronPython should convert the<o:p></o:p></span></p><p class=MsoPlainText><span style='font-size:11.0pt;font-family:"Calibri","sans-serif"'>> function to the delegate type on the call.<o:p></o:p></span></p><p class=MsoPlainText><span style='font-size:11.0pt;font-family:"Calibri","sans-serif"'><o:p> </o:p></span></p><p class=MsoPlainText><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:black'>Unfortunately it doesn't seem to </span><span style='font-size:11.0pt;font-family:Wingdings;color:black'>L</span><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:black'>. I repro'd that it doesn't on the desktop as well as in Silverlight; here’s a desktop repro:<o:p></o:p></span></p><p class=MsoPlainText><span style='color:black'><o:p> </o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'>>>> import clr<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'>>>> clr.AddReferenceToFile("pyext")<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'>>>> import pyext<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'>>>> t = pyext.TestClass()<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'>>>> def foo(s, e): pass<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'>...<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'>>>> import System<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'>>>> t.AttachEvent("foo", System.EventHandler(foo))<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'>foo hooked!<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black;background:yellow;mso-highlight:yellow'>>>> t.foo += foo<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black;background:yellow;mso-highlight:yellow'>Traceback (most recent call last):<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black;background:yellow;mso-highlight:yellow'> File "<stdin>", line unknown, in <module><o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black;background:yellow;mso-highlight:yellow'>TypeError: unsupported operand type(s) for +=: 'MyEvent' and 'function'</span><span style='color:black'><o:p></o:p></span></p><p class=MsoPlainText><span style='color:black'><o:p> </o:p></span></p><p class=MsoPlainText><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:black'>Also, when using a EventHandler as the right-hand-side of the +=, the AttachEvent method gets called, but still complains that the “foo” attribute doesn’t exist:<o:p></o:p></span></p><p class=MsoPlainText><span style='color:black'><o:p> </o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black;background:yellow;mso-highlight:yellow'>>>> t.foo += System.EventHandler(foo)<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black;background:yellow;mso-highlight:yellow'>foo hooked!<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black;background:yellow;mso-highlight:yellow'>Traceback (most recent call last):<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black;background:yellow;mso-highlight:yellow'> File "<stdin>", line unknown, in <module><o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black;background:yellow;mso-highlight:yellow'>AttributeError: 'TestClass' object has no attribute 'foo'</span><span style='color:black'><o:p></o:p></span></p><p class=MsoPlainText><span style='color:black'><o:p> </o:p></span></p><p class=MsoPlainText><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:black'>Any ideas? Here’s the pyext.cs stub that I used:<o:p></o:p></span></p><p class=MsoPlainText><span style='color:black'><o:p> </o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'>//<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'>// pyext.cs<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'>//<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'><o:p> </o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'>using Microsoft.Scripting.Runtime;<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'>using System;<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'>using Microsoft.Scripting.Utils;<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'>using System.Runtime.CompilerServices;<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'><o:p> </o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'>[assembly: ExtensionType(typeof(pyext.TestClass), typeof(pyext.TestClassExtension))]<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'><o:p> </o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'>namespace pyext {<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> public class TestClass {<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> public void AttachEvent(string eventName, EventHandler handler) {<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> Console.WriteLine(string.Format("{0} hooked!", eventName));<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> }<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> }<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'><o:p> </o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> public static class TestClassExtension {<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> [System.Runtime.CompilerServices.SpecialNameAttribute]<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> public static object GetBoundMember(TestClass obj, string name) {<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> return new MyEvent(obj, name);<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> }<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> }<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'><o:p> </o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> public class MyEvent {<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> private readonly TestClass _obj;<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> private readonly string _event;<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'><o:p> </o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> internal MyEvent(TestClass obj, string eventStr) {<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> _obj = obj;<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> _event = eventStr;<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> }<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'><o:p> </o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> [System.Runtime.CompilerServices.SpecialNameAttribute]<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> public object op_AdditionAssignment(EventHandler func) {<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> _obj.AttachEvent(_event, func);<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> return null;<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> }<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'> }<o:p></o:p></span></p><p class=MsoPlainText style='margin-left:.5in'><span style='color:black'>}<o:p></o:p></span></p></div></div></body></html>