hi,
I'm trying to dynamically overload an objects methods
with IP. Something like:
class A:
def test(self):
print "A"
a = A()
def newtest(self):
print "newtest"
testType = type(a.test)
a.test = testType(newtest, a)
a.test()
prints "newtest" as expected. However I cannot overload
a ReflectedMethod made in C# this way (or don't know how).
A workaround for this, is to implement the methods in a
derived class and override them them there. However these
overloaded methods cannot be called. I have attached a
C# and a Python file to reproduce this.
Any Ideas? Is this supposed to work?
l8r...
Thomas
using System;
using System.Collections;
namespace DynamicTest {
public class Base {
protected int data;
public Base(int data) {
this.data = data;
}
public virtual String virtual_test1() {
return virtual_test2();
}
public virtual String virtual_test2() {
return virtual_test3();
}
public virtual String virtual_test3() {
return "Base.virtual_test3";
}
}
}
import sys
#sys.LoadAssemblyFromFile("c:\dev\IronPython-1.0-Beta1\Tutorial\DynamicTest.dll")
sys.LoadAssemblyFromFile("DynamicTest.dll")
from DynamicTest import Base
b = Base(0)
class Extend(Base):
def virtual_test3(self):
return "Extend.virtual_test3"
def virtual_test2(self):
return self.virtual_test3()
def test(self):
return "test"
e1 = Extend(1)
e2 = Extend(2)
print "%s == %s == %s" % (b.virtual_test1(), b.virtual_test2(),
b.virtual_test3())
print "%s == %s == %s" % (e1.virtual_test1(), e1.virtual_test2(),
e1.virtual_test3())
print "%s == %s == %s" % (e2.virtual_test1(), e2.virtual_test2(),
e2.virtual_test3())
def newtest(self):
return "New Method"
# This throws an exception:
# CallType = type(e1.virtual_test1)
# e1.virtual_test1 = CallType(newtest, e1)
CallType = type(e1.virtual_test3)
e1.virtual_test3 = CallType(newtest, e1)
CallType = type(e2.virtual_test3)
e2.virtual_test2 = CallType(newtest, e2)
# This works
print "%s == %s == %s" % (e1.virtual_test1(), e1.virtual_test2(),
e1.virtual_test3())
# Wow! Can't call an overloaded method from C# :(
print "%s == %s" % (e2.virtual_test1(), e2.virtual_test2())
_______________________________________________
users mailing list
[email protected]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com