Skip to content

Instantly share code, notes, and snippets.

@phrohdoh
Created October 15, 2014 06:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phrohdoh/eabf4d4f8f121f28ce6a to your computer and use it in GitHub Desktop.
Save phrohdoh/eabf4d4f8f121f28ce6a to your computer and use it in GitHub Desktop.
IronPython scripting for C#
using System;
namespace CSharpLib
{
public class CSharpClass
{
public int Add(int a, int b)
{
return a + b;
}
public void Write(string print)
{
Console.Write(print);
}
}
}
using System;
using System.Dynamic;
using System.Collections.Generic;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
namespace IronPythonCSharp
{
public class MainClass
{
static void Main()
{
var ipy = Python.CreateRuntime();
var engine = ipy.GetEngine("Python");
var paths = engine.GetSearchPaths();
paths.Add(AppDomain.CurrentDomain.BaseDirectory);
engine.SetSearchPaths(paths);
dynamic test = ipy.UseFile("my.py");
// This calls my.py's Py_Write(string)
// test.Py_Write("csharp to ip");
}
}
}
# -*- coding: utf-8 -*-
import sys
import clr
clr.AddReference("IronPython")
clr.AddReference("CSharpLib")
from IronPython.Hosting import Python
from CSharpLib import CSharpClass as csc
cscT = csc()
def main():
i = Add(cscT, 100, 50)
Write(cscT, "%d\n" %(i))
Write(cscT, "---- --- -- -✈\n")
def Add(csc, a, b):
return csc.Add(a, b)
def Write(csc, string):
csc.Write(string)
# This would be called via C#
# def Py_Write(string):
# print string
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment