Skip to content

Instantly share code, notes, and snippets.

@mayuki
Created March 6, 2009 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayuki/74896 to your computer and use it in GitHub Desktop.
Save mayuki/74896 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Text;
using System.Collections;
using System.Reflection;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
namespace DLRHostingTestProject
{
public class Program : MarshalByRefObject
{
public event EventHandler TestEvent;
static void Main(string[] args)
{
ScriptRuntimeSetup scriptRuntimeSetup = new ScriptRuntimeSetup();
scriptRuntimeSetup.LanguageSetups.Add(new LanguageSetup("IronPython.Runtime.PythonContext, IronPython", "IronPython", new [] { "IronPython", "Python", "py"}, new [] { ".py"}));
ScriptRuntime scriptRuntime = new ScriptRuntime(scriptRuntimeSetup);
Program program = new Program();
scriptRuntime.LoadAssembly(Assembly.LoadWithPartialName("System.Windows.Forms"));
scriptRuntime.Globals.SetVariable("HostProgram", program);
ScriptScope scope = scriptRuntime.CreateScope();
//var retVal = scriptRuntime.GetEngine("py").Execute("1 + 2", scope);
var scriptSource = scriptRuntime.GetEngine("py").CreateScriptSourceFromString(
@"
a = 1 + 1
b = a + 10
import clr
import HostProgram
from System.Windows.Forms import *
def OnTestEvent(sender, e):
MessageBox.Show('hauhau')
def Hauhau():
return True
HostProgram.TestEvent += OnTestEvent
", SourceCodeKind.Statements);
ScriptScope scriptScope = scriptRuntime.CreateScope();
var retVal = scriptSource.Execute(scriptScope);
ObjectHandle funcObj = scriptSource.Engine.GetVariableHandle(scriptScope, "Hauhau");
retVal = scriptRuntime.GetEngine("py").Operations.Call(funcObj, new object[0]).Unwrap();
//program.TestEvent(program, EventArgs.Empty);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment