C#ããIronPython2.6ãå¼ã³åºãæ¹æ³
IronPython2.6RC3が出ているã¨ããã®ã«ãC#ããIronPythonãå¼ã³åºãã¦ä½¿ãæ¹æ³ã«ã¤ãã¦ã¯IronPython1.1ã®æ
å ±ã ã£ãã2.0ç³»åãå¤ãã®ã§ã¡ããã¨ã¡ã¢ãæ®ãã¦ããã¾ãã
IronPythonèªä½ã®ããã¥ã¢ã«ã«è¼ãã¦ãããã°ãããã§ããããã¥ã¼ããªã¢ã«ã¨ãã«ã¯è¼ã£ã¦ãªãã¿ããã§ãããããããDLRããã¹ãã£ã³ã°ããããä¸æ®µæ½è±¡åããã¨ãããæ¢ãã«è¡ãã°è¦ã¤ãããããªæ°ããã¾ããå¾ã§*1ã
ã¨ãããã確èªããã®ã¯ä»¥ä¸ã®ãããªãã¨ã
- C#ããIronPythonã®ã³ã¼ããåçã«å®è¡
- IronPythonã§ä½¿ãããã¢ã»ã³ããªã¸ã®åç §ãC#ã§è¿½å ãã
- C#ã®ã¢ã»ã³ããªå ã§å®ç¾©ããinterfaceãå®è£ ããï¼ç¶æ¿ããï¼
- IronPythonã®å¤æ°ãC#ã§åãåãã»ã»ãããã
- IronPythonã®é¢æ°ãC#ããå¼ã³åºãã¦è¿ãå¤ãåå¾ãã
- IronPythonã§å®ç¾©ããã¯ã©ã¹ãC#ã§å®ä½åãã
ã¾ãã¯Visual Studio*2ãèµ·åãã¦C#ã®ãã³ã³ã½ã¼ã«ã¢ããªã±ã¼ã·ã§ã³ãã®ããã¸ã§ã¯ããä¸ã¤ä½ãã¾ããï¼ãã¡ãããWindowsãã©ã¼ã ã¢ããªã±ã¼ã·ã§ã³ãã§ãOKã§ãï¼
次ã«ãä¸è¨URLããIronPython-2.6-Bin.zipããã¦ã³ãã¼ããã¾ããããä¸ã«ããã¤ãã®dllãå
¥ã£ã¦ããã®ã確èªããããå
ã»ã©ã®ããã¸ã§ã¯ãã«å¿
è¦ãªåç
§ã追å ãã¾ãããã
以ä¸ã®dllã®åç
§ã追å ãã¾ãã
- IronPython.dll
- Microsoft.Scripting.dll
- Microsoft.Scripting.Core.dll
ãããªæãã§ãã
ããã§å®è¡ãããã¨ããã¨ãdllã足ããã¼ãã¨æãããã®ã§ã次ã®dllãå®è¡ãã¡ã¤ã«ãã§ãããã©ã«ãã«å
¥ãã¦ããã¾ãã
- IronPython.Modules.dll
- Microsoft.Dynamic.dll
- Microsoft.Scripting.ExtensionAttribute.dll
使ãããªãMicrosoft.Scripting.Debugging.dllã¨ããã®ãããã¾ãããæ®éã«å®è¡ããåã«ã¯ç¡ãã¦ãæ¯éããã¾ãã*3ã
ã§ãã£ã¦å¾ã¯ã³ã¼ããæ¸ãã ãã§ãã
IronPythonå´ (Scripts/Sample.py)
# -*- coding: utf-8 -*- import clr # C#ã®ã¢ã»ã³ããªãåç §ãã¦ãã # æ¬æ¥å¿ è¦ãªclr.addReferenceã®å¦çã¯C#å´ã§ # å ã«ãã£ã¦ãã¾ã£ã¦ãã®ã§æ®éã«importããã ãã§è¯ã from IronTest import * print 'Sample.py Called' # C#ã§ã»ããããå¤æ°ã表示ãã print message_from_csharp # C#å´ã§åç §ãããå¤æ° message_from_python = 'Hello IronPython' def test(num): print "test func called: value %d" % num return "test result" # C#å´ã§å®ç¾©ããã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ãã class TestPlugin(IPluginBase): def install(self): print "install called"
C#å´
using System.Reflection; using System; using System.IO; using Microsoft.Scripting.Hosting; using IronPython.Hosting; namespace IronTest { public interface IPluginBase { void install(); } class Program { static void Main(string[] args) { // ã¹ã¯ãªãããã¡ã¤ã«ã®ãã¹ãæ±ãã Assembly assembly = typeof(Program).Assembly; string dirPath = Path.GetDirectoryName(assembly.Location); string scriptPath = Path.Combine(dirPath, "Scripts"); string filePath = Path.Combine(scriptPath, "Sample.py"); // Python ã¹ã¯ãªããã¨ã³ã¸ã³çæ ScriptEngine engine = Python.CreateEngine(); // ãã®ã¢ã»ã³ããªãPythonå´ã§åç §ã§ããããã«ãã¦ãã // ãã¡ããå¥ã®ã¢ã»ã³ããªã«ãã¦ãããåç §ããããã«ãã§ãã ScriptRuntime runtime = engine.Runtime; runtime.LoadAssembly(Assembly.LoadFile(assembly.Location)); // Python ã¹ã¯ãªãããèªã¿è¾¼ã ScriptSource source = engine.CreateScriptSourceFromFile(filePath); CompiledCode code = source.Compile(); // ã¹ã³ã¼ãçæ // IronPython ã«å¤æ°ã渡ãããåå¾ãããããã¨ãã«ä½¿ã ScriptScope scope = engine.CreateScope(); // Pythonå´ã®ã¹ã³ã¼ãã«å¤æ°ãã»ãããã scope.SetVariable("message_from_csharp", "Hello CSharp"); // Python ã¹ã¯ãªããå®è¡ï¼é¢æ°å®ç¾©ãã¯ã©ã¹å®ç¾©ãå®äºããï¼ code.Execute(scope); // ã¹ã¯ãªããã®ã¹ã³ã¼ãå ã§çæãããå¤æ°ãåå¾ Console.WriteLine(scope.GetVariable<string>("message_from_python")); // Pythonå´ã§å®ç¾©ããé¢æ°ãå¼ã³åºã Console.WriteLine(engine.Execute("test(100)", scope)); // Pythonå´ã§å®ç¾©ããã¯ã©ã¹ãã¤ã³ã¹ã¿ã³ã¹å IPluginBase plugin = (IPluginBase)engine.Execute("TestPlugin()", scope); plugin.install(); // enterãæ¼ãããã¾ã§å¾ æ© Console.ReadLine(); } } }
å®è¡çµæ
Sample.py Called Hello CSharp Hello IronPython test func called: value 100 test result install called
é¢æ°å¼ã³åºãã¯engine.Executeã«ããPythonã³ã¼ãã®å®è¡ãªã®ã§ããªãã¸ã§ã¯ããå¼æ°ã«æ¸¡ãã®ã¯é£ãããã§ãããåºæ¬çãªä½¿ãæ¹ã¨ãã¦ã¯2.0ç³»åã¨å¤ãã£ã¦ãªãã¿ããã§ãã
ä¹ ãã¶ãã«æ¸ãããã¯ã¦ãªã®ã¹ã¼ãã¼ãã¬è¨æ³ã®æ¸ãæ¹ãå¿ãã¦ãï¼æ±ï¼
åè
*1:ç¡ããã°IronPythonèªä½ã®ã½ã¼ã¹ãèªã¿ã¾ããã
*2:ç¡ã人ã¯ç¡åã®Visual C# 2008 Express Editionãè½ã¨ãã¾ããã
*3:ãããã°ç¨ã®é¢æ°ã¨ããå ¥ã£ã¦ãæ°ããã¾ããããªãã¡ã¬ã³ã¹ãè¦ã¤ãããªãã®ã§ãããã¾ãã