Created
October 15, 2014 06:10
-
-
Save phrohdoh/eabf4d4f8f121f28ce6a to your computer and use it in GitHub Desktop.
IronPython scripting for C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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