c# - 在 python 脚本中从 C# dll 创建一个通用列表

标签 c# python

var settings = new SettingsClass();
settings.SpecificValue = new List<SpecificValueClass>();

这是我的 C# 项目中的代码示例。

我有一个 python 脚本,它使用此代码示例导入 .dll,我想在 python 脚本中创建此 settings.SpecificValue 变量。

如果不在 C# 中创建一个我可以在 python 代码中调用的函数,是否有可能。

public class SettingsClass
{
    public bool Timesync { get; set; }
    public string SystemName { get; set; }
    public string Timezone { get; set; }
    public List<SpecificValueClass> SpecificValue{ get; set; }
}

public class SpecificValueClass
{
    public string ID { get; set; }
    public int value { get; set; }
}

在 python 中我想这样调用它:

settings = SettingsClass() <-- this call is no problem and I can do it with a dotNet library in python
settings.SystemName = systemname
settings.Timesync = timesync
settings.Timezone = "%s//%s" % (timezone1,timezone2)
settings.SpecificValue = ... <-- not sure how to make this to create a empty List / empty whatever so I can add 2 objects of the type SpecificValueClass 

(也可以创建 2 个对象 ob SpecificValueClass) 所以我无法开始工作的唯一部分是 settings.SpecificValue 的初始化,因为它是一个通用列表 ...

任何帮助将不胜感激

最佳答案

终于使用 TestComplete 的 dotNet 库让它工作,但我想它与 python 的 clr 包一样。只需自己编写泛型部分,通常 C# 会处理标签:

typeListOf = dotNET.System.Type.GetType("System.Collections.Generic.List`1")
objType = dotNET.namespace.SpecificValueClass.zctor().GetType()
paramTypes = dotNET.System.Array.CreateInstance(dotNET.System.Type.GetType("System.Type"), 1)
paramTypes.SetValue(objType, 0)
typeListOfString = typeListOf.MakeGenericType(paramTypes)

newValue = dotNET.namespace.WattPeakValues.zctor()

newValue.ID = 1
settings.SpecificValue.Add(newValue)

关于c# - 在 python 脚本中从 C# dll 创建一个通用列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56172144/

相关文章:

c# - 如何将一个 COM 类实例作为参数传递给 C# 中的另一个 COM 方法?

c# - 找不到 MVVM EventToCommand

c# - CredentialCache.Add 方法中的 authType 参数

c# - 以反向模式运行 MongoDB 集合

python - 将 Keras 模型导出到 TensorFlow 时,“顺序”对象没有属性 '_is_graph_network'

python - 有没有办法在不满足条件的情况下替换值以避免 nan?

c# - 存储客户端列表对象

python - 如何使用机器学习从扫描文档中提取指定片段

python - Flask中的路径路由

python - 在bash中输入 `ls`时如何隐藏.pyc文件