IronPython 广泛使用System.Reflection。它会自动导入对其可用的类型和方法。
您可以在 DLL 类库中公开您的类型。
构建 DLL 后,您可以将其导入“IronPython”
import clr
clr.AddReferenceToFileAndPath(r"C:\SomeFolder\SomeClassLibrary.dll")
您还可以动态加载 DLL 和导入命名空间:
sys.path.append(r"C:\SomeFolder")
clr.AddReference ("SomeClassLibrary.dll") # Your Class libary
import SomeNamspace # import SomeNamespace from your dll.
在您的库中公开您的事件:
Public Class SomeClass
Public Event SomeEvent ....
然后您可以在 `IronPython
中使用它们
def SomeHandleHandler(*args):
pass
Foo.SomeEvent += SomeHandleHandler
要执行相反的操作并使用IronPython 触发事件,您只需在您的 Vb.Net 代码中添加处理程序。
这是工作代码:
类库 MyClassLibary 中的 MyIpyClass.vb
Public Class MyIpyClass
Public Event MyEvent()
Public Sub New()
AddHandler MyEvent, HandleMyEvent()
End Sub
Private Function HandleMyEvent() as MyEventEventHandler
Console.WriteLine("Raised My Event")
Return Nothing
End Function
Public Sub RaiseMyEvent()
RaiseEvent MyEvent()
End Sub
End Class
IronPython 代码:
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
clr.AddReferenceToFileAndPath("C:\\MyDllPath\\MyClassLibary.dll")
import MyClassLibary
from System.Windows.Forms import *
from System.Drawing import *
def mevent(*args): print("Event From IPY") # create IPython event hander for MyIpyClass.MyEvent
m = MyClassLibary.MyIpyClass() # create new instance of my class
m.MyEvent += mevent # Add IPythong Handler
m.RaiseMyEvent() # For Simplicity, Call MyIpyClass.MyEvent.RaiseEvent to raise the event
f = Form() # Create a Form to handle some events from System.Windows.Forms
f.Text = "My Form"
#defne a form click handler to print out the event args
def click(*args): print args
f.Click += click
b = Button() # Create a button and add it to the form
b.Text = "GO"
b.Name = "GO"
b.Location = Point(10,10)
#create a button click handler that prints hellow word
def click(f, a):
print("hello world")
#assign the button click handler
b.Click += click
#add the button to the form
f.Controls.Add(b)
#call Form.ShowDialog() - Show() blocks and will hang the form.
f.ShowDialog()