【发布时间】:2017-07-14 23:42:54
【问题描述】:
我需要运行一段python代码,这段代码会生成一个嵌套字典{"str","list"},里面的列表有很多类型,比如int或者bool
我需要在 c# 代码中使用这个字典,我需要这个字典和在 python 中的完全一样。
我做了这个
try:
#create a dictionary of all tests stats
resultStat = ResDesc.__dict__
#convert this dictionary from python type to c# generic type so
SQSManagedUI.dll can handle the information contained inside
resultStatDict = Dictionary[String,String]()
#unify the key value pairs to be all strings, even the Boolean
values will be converted #to 'Str', so DLLs will be able to
process it.
for k,v in resultStat.items():
if isinstance(v,str):
resultStatDict[k]=v
else:
v= str(v)
resultStatDict[k]=v
#send the dictionary to SQSmanagedUI dll for proces sing as
Dict[string:string]
resultWindow=SQSManagedUI.Dialogs.ShowResultManager(resultStatDict)
except:
traceback.print_exc()
但这给了我一个带有字符串/字符串对的字典,我可以做些什么来将我的类型保留在列表中(bool、str、int)。
我对 c# 中的反射有点了解,我在我的 python 代码中使用 Iron Python。
谁能帮忙,先谢谢了。
【问题讨论】:
-
你能发布一个例子来说明你的 dict 的样子吗?
-
我不确定 C#/IronPython 如何互操作,但 C# 支持 dynamic。它的用途之一是支持像 python 这样的语言(甚至在页面上提到)。
标签: c# python ironpython