我希望你们中的一些人能够帮助我,因为我正在用这个方法结束我的智慧。看来我可以添加instancemethod委托,但请注意删除它们。对代表的对象引用是相同的,当然?IronPython实例方法无法从CLR代理中移除
这里是错误的蒸馏水再现:鉴于这种简单的小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);
}
}
这按预期工作:
## -- 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
为做到这一点:
## -- 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
然而,这不起作用:
## -- 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
看起来,因为它们传递到功能和不同的对象引用正在调用列表以某种方式被改变的实例方法。我感到我失去了一些愚蠢的东西!
干杯,
约翰