我使用Iron Python作爲免費的附加工具來測試將通信包裝到某些定製硬件的API,以便非開發團隊可以通過python與硬件一起玩。使用來自IronPython的可變參數調用CLR方法
但是我無法弄清楚如何讓.NET void func(params object[] args)
映射到Python def (*args)
。
這裏有一些代碼來解釋。
我有一個類型,允許注入日誌回調格式和處理消息是在Console.WriteLine和Debug.WriteLine的簽名之後。
public class Engine
{
Action<string, object[]> _logger;
public void Run()
{
Log("Hello '{0}'", "world");
}
public void SetLog(Action<string, object[]> logger)
{
_logger = logger;
}
void Log(string msg, params object[] args)
{
_logger(msg, args);
}
}
在我的IronPython代碼
import clr
from System import *
from System.IO import Path
clr.AddReferenceToFileAndPath(Path.GetFullPath(r"MyAssembly.dll"))
from MyNamespace import *
def logger(msg, *args):
print(String.Format(msg, args))
print(String.Format(msg, list(args)))
print(String.Format(msg, [args]))
a=[]
for i in args:
a.append(i)
print(String.Format(msg, a))
e = Engine()
e.SetLog(logger)
e.Run()
輸出
Hello '('world',)'
Hello 'IronPython.Runtime.List'
Hello 'IronPython.Runtime.List'
Hello 'IronPython.Runtime.List'
我想
Hello 'world'
可能重複array](http://stackoverflow.com/questions/1107789/assigning-a-iron-python-list-to-net-array) – 2012-08-17 12:54:53
如果它是該職位的副本那麼要麼我不明白我自己的問題或者我不理解他們的(或兩者:-) – 2012-08-17 13:17:14
我認爲@HansPassant引用的問題不是一個硬複本,而是暗示其中一個可能的解決方案(請參閱我的答案和答案rsion到'Array [object]')。 – 2012-08-19 12:28:27