[IronPython] Add/remove python function to C# event in C#

Dan Eloff dan.eloff at gmail.com
Mon Jul 14 03:12:06 CEST 2008


On Sun, Jul 13, 2008 at 7:20 PM, Dino Viehland
<dinov at exchange.microsoft.com> wrote:
> Ahh I see what you mean now.  If you're doing this from the hosting APIs you can use the ObjectOperations class to do the conversion:
>
> someObj.someEvent += objOps.ConvertTo<DelegateType>(obj);

That is disturbingly easy. I knew you'd know the answer.

But I wonder if that would work for removing a handler from an event?

someObj.someEvent += objOps.ConvertTo<DelegateType>(obj);
someObj.someEvent -= objOps.ConvertTo<DelegateType>(obj);

One workaround might be to cache the delegate on the way in and use
the cached delegate to remove it.

_cache[obj] = objOps.ConvertTo<DelegateType>(obj);
someObj.someEvent += _cache[obj]

someObj.someEvent -= _cache[obj]

But maybe there's a better way, or maybe I'm daft and it'll just work.

-Dan



More information about the Ironpython-users mailing list