プログラミングなど

ラベル C# の投稿を表示しています。 すべての投稿を表示
ラベル C# の投稿を表示しています。 すべての投稿を表示

2009年5月22日金曜日

C# + Python embedding C#のクラスをPython側で使用する

C#のクラスをIronPython側で使うのは、非常に簡単だ。
(CPythonだと、結構、面倒だったような...)

---- コード(C#側) ----

using System;

using IronPython;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;

namespace python_test001
{
 class Program
 {
  public static void Main(string[] args)
  {
   try
   {
    ScriptEngine engine = Python.CreateEngine();
    ScriptScope scope = engine.CreateScope();

    // C#と変数のやり取りをするPythonのファイルを設定
    ScriptSource source =
     engine.CreateScriptSourceFromFile("test.py");

    TestNumber test = new TestNumber();
    // Python側の変数をTestDataとする
    scope.SetVariable("num", test);

    // 実行
    source.Execute(scope);

    int i = test.Number;

    // 実行後のPython側の変数値を取得

    // 結果を出力 ==> 15
    System.Console.WriteLine(test.Number);
   }
   catch (Exception ex)
   {
    System.Console.WriteLine("Error:"
     + ex.Message.ToString());
   }
  }
 }
}

// Python側に渡すクラス
class TestNumber {
 int number = 0;
 public void Plus(int i) {
  number += i;
 }
 public int GetNumber() {
  return this.number;
 }
 public void SetNumber(int i) {
  this.number = i;
 }
 public int Number {
  get {
   return this.number;
  }
  set {
   this.number = value;
  }
  
 }
}

---- コード(Python側) ----

num.Number = 10
print 'py_number:', num.GetNumber()
#プロパティのgetがprotectedメンバだとかでエラー
#print 'py_number:', num.Number
num.Plus(5)

================================

■参考
貧脚レーサーのサボり日記 - C#でIronPython2.0をホスティングする
Embedding the Dynamic Language Runtime
The IronPython Calculator and the Evaluator

2009年5月17日日曜日

C# + Python embedding ファイルの変数のやりとり

Pythonファイルとのやりとりの例

■.NET Framework2.0 sp1
■IronPython 2.0

---- コード(Python側) ----

result = param1 + param2


---- コード(C#側) ----

using System;

using IronPython;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;

...

  public static void Main(string[] args)
  {
   try
   {
    ScriptEngine engine = Python.CreateEngine();
    ScriptScope scope = engine.CreateScope();

    // C#と変数のやり取りをするPythonのファイルを設定
    ScriptSource source =
     engine.CreateScriptSourceFromFile("test.py");

    // Python側の変数に値を設定
    scope.SetVariable("param1", 1);
    scope.SetVariable("param2", 10);
    
    // 実行
    source.Execute(scope);
    
    // 実行後のPython側の変数値を取得
    
    // 結果を出力 ==> 1
    object arg1 = scope.GetVariable("param1");
    System.Console.WriteLine(arg1);
    // 結果を出力 ==> 10
    object arg2 = scope.GetVariable("param2");
    System.Console.WriteLine(arg2);
    // 結果を出力 ==> 11
    object res = scope.GetVariable("result");
    System.Console.WriteLine(res);
   }
   catch (Exception ex)
   {
    System.Console.WriteLine("Error:"
     + ex.Message.ToString());
   }
  }

================================

C#で Pythonファイル(test.py)内の変数の値の設定、取得を行います。

Python側では、あらかじめ変数に値が設定されている必要はありません。

C#で Python側の変数param1, param2 に、それぞれ値を設定し、Python側が実行された後、ython側の計算結果のresult変数をC#で取得しています。

C# + Python embedding 簡単な計算

CC#からIronPythonに計算させる簡単な例。

■.NET Framework2.0 sp1で実行
 sp1でないと動かない点に注意
■IronPython 2.0
 以下からIronPython-2.0.1-Bin.zipをダウンロード
 http://www.codeplex.com/IronPython → Downloads

 以下を参照設定で追加
  IronPython.dll
  IronPython.Modules.dll
  Microsoft.Scripting.Core.dll
  Microsoft.Scripting.dll
  Microsoft.Scripting.ExtensionAttribute.dll

---- コード ----

using System;

using IronPython;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;

...

    public static void Main(string[] args)
    {
      try
      {
        ScriptEngine engine = Python.CreateEngine();
        ScriptScope scope = engine.CreateScope();

        // 1 + 2 を Python に計算させる
        ScriptSource source =
          engine.CreateScriptSourceFromString(
            "1 + 2", SourceCodeKind.Expression);
        object res = source.Execute();
        // 結果を出力 ==> 3
        System.Console.WriteLine(res);
      }
      catch (Exception ex)
      {
        System.Console.WriteLine("Error:" +
          ex.Message.ToString());
      }
    }