Skip to content

Instantly share code, notes, and snippets.

@ironpythonbot
Created December 9, 2014 17:53
Show Gist options
  • Save ironpythonbot/ae94c784547332708d21 to your computer and use it in GitHub Desktop.
Save ironpythonbot/ae94c784547332708d21 to your computer and use it in GitHub Desktop.
CodePlex Issue #30338 Plain Text Attachments
## -- Procedural functions (function) work. ---
a = App.TypedEvent[object]()
def test(s):
print s
a.removeHandler(test)
a.addHandler(test)
a.addHandler(test)
# Output
a.invoke("Hello")
>>> Hello
a.invoke("Hello")
>>> Hello
a.invoke("Hello")
>>> Hello
## -- Static methods (unbound) work. ---
a = App.TypedEvent[object]()
class Foo:
@staticmethod
def test(s):
print s
a.removeHandler(Foo.test)
a.addHandler(Foo.test)
a.addHandler(Foo.test)
# Output
a.invoke("Hello")
>>> Hello
a.invoke("Hello")
>>> Hello
a.invoke("Hello")
>>> Hello
## -- Instance methods (bound) do not work. --
a = App.TypedEvent[object]()
class Foo:
def test(self, s):
print s
a.removeHandler(self.test)
a.addHandler(self.test)
f = Foo()
a.addHandler(f.test)
# Output
a.invoke("Hello")
>>> Hello
a.invoke("Hello")
>>> Hello
>>> Hello
a.invoke("Hello")
>>> Hello
>>> Hello
>>> Hello
>>> Hello
-------------- C#
public class TypedEvent<T1> : TypedEventBase
{
/** A definition of the function signature. */
public delegate void ActionSignature(T1 kParam1);
/** @brief A reference to the delegate which stores our handles. */
protected ActionSignature pAction = null;
public virtual bool addHandler(ActionSignature kHandler)
{
// If we are already contained in the list then we don't need to be added again.
if (pAction != null)
{
if (this.pAction.GetInvocationList().Contains(kHandler))
return false;
}
// Add us to the list and return success.
this.pAction += kHandler;
return true;
}
public virtual bool removeHandler(ActionSignature kHandler)
{
// If we have no handles return false.
if (pAction == null)
return false;
// If we do not contain the handler then return false.
if (!this.pAction.GetInvocationList().Contains(kHandler))
return false;
// Remove the handler and return true.
this.pAction -= kHandler;
return true;
}
public void invoke(T1 kParam1)
{
if (this.pAction != null)
this.pAction(kParam1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment