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/