【问题标题】:Python dictionary will be sent to c# dll, dynamic vs. static types issuePython 字典将被发送到 c# dll,动态与静态类型问题
【发布时间】: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


【解决方案1】:

要使用 python 操作,您可以利用 C# 对 dynamic 类型的支持。

动态类型简化了对 COM API(例如 Office 自动化 API)、动态 API(例如 IronPython 库)和 HTML 文档对象模型 (DOM) 的访问。

【讨论】:

  • @Ahmed 不客气,很抱歉我的回答很短,但我自己从未使用dynamic 与 python 进行互操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-23
  • 2014-03-30
  • 1970-01-01
  • 2010-12-02
  • 1970-01-01
  • 2010-10-15
  • 1970-01-01
相关资源
最近更新 更多