19th August 2022 at 2:56pm
IronPython 2.7のころの記事です。
IronPythonとC#で変数をやり取りする時に、C#ではどう見えるのか、どう変換されるのか?
これは、IronPythonとC#の連携を行う際には重要な質問です。IronPythonと違い、C#は変数を定義する際に型を定義する必要があるからです。
C#にはGetType()関数があるのでこれを使って確認してみます。
こちらにサンプルプログラムを作成しました。
以下のように、GetType()を利用したGetTypeCS()とGetTypeCs_IList()を作成しました。
IronPythonのリストは、C#の関数の引数をIList型にしておけば渡すことが可能です。
public static string GetTypeCs_IList(IList<object> obj)以下、プログラムです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyUtil
{
public class MyFunction
{
public static string GetTypeCs(object obj)
{
try
{
return obj.GetType().ToString();
}
catch (Exception ex)
{
string msg = "Exception " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message;
throw new ApplicationException(msg);
}
}
public static string GetTypeCs_IList(IList<object> obj)
{
try
{
return obj[0].GetType().ToString();
}
catch (Exception ex)
{
string msg = "Exception " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message;
throw new ApplicationException(msg);
}
}
}
}GetTypeCs.slnを開きReleaseビルドしてから、ReleaseフォルダをVisual Studio Codeで開き、example_GetTypeCs.pyを実行してください。

IronPythonのLongは、C#ではSystem.Numerics.BigIntegerのようです。