Alright, so I have this python code
_events.GameStart += method
method():
DoStuff
and on the c# side
[SecuritySafeCritical]
public class ScriptEvents: MarshalByRefObject
{
public event Action GameStart;
public ScriptEvents(Engine e)
{
_engine = e;
}
public void FireGameStart()
{
if(GameStart != null)
GameStart.Invoke();
}
}
So I pass in ScriptEvents
as _events
. This has worked fine for python calling methods like _events.FireGameStart()
, but I can't attach to the event.
I end up getting an error saying the delegate can't be serialized. Now I've tried using CrossAppDomainDelegate
and that doesn't seem to solve the problem.
I was wondering if there was some magical way to toss that delegate across the wall so that I can invoke it from the c# side and have the subsequent python code activate?
Also, in case it's relevant
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);
}