好的,我有这个python代码
_events.GameStart += method
method():
DoStuff
并在c#侧
[SecuritySafeCritical]
public class ScriptEvents: MarshalByRefObject
{
public event Action GameStart;
public ScriptEvents(Engine e)
{
_engine = e;
}
public void FireGameStart()
{
if(GameStart != null)
GameStart.Invoke();
}
}
所以我将ScriptEvents
作为_events
传递给我。这对于_events.FireGameStart()
这样的python调用方法很有效,但是我无法附加到事件中。
我最终收到一条错误消息,指出代理无法序列化。现在我尝试使用CrossAppDomainDelegate
,这似乎无法解决问题。
我想知道是否有一些神奇的方法可以将该委托交给墙,以便我可以从c#侧调用它并激活后续的python代码?
另外,如果它是相关的
var Events = new ScriptEvents();
AppDomain sandbox = CreateSandbox(forTesting);
var _engine = Python.CreateEngine(sandbox);
ScriptScope scope = _engine.CreateScope();
scope.SetVariable("_events",Events);
var _sponsor = new Sponsor();
var life = (ILease) RemotingServices.GetLifetimeService(Events);
life.Register(_sponsor);
private static AppDomain CreateSandbox(bool forTesting)
{
var permissions = new PermissionSet(PermissionState.Unrestricted);
var appinfo = new AppDomainSetup {ApplicationBase = AppDomain.CurrentDomain.BaseDirectory};
return AppDomain.CreateDomain("Scripting sandbox", null, appinfo, permissions);
}