C# - 将列表 System.Object[] 转换为列表 System.Int32[]

C# - Convert a List System.Object[] to List System.Int32[]

我是 C# 新手,

我正在尝试使用已创建的函数 here

实际上我不知道如何return 将列表作为数组作为参数。 这里的这个函数将使用 Odoo API.

的“write”方法
    public void SetFieldValue(string field, object value)
    {
        var fieldAttribute = _fieldsResult.FirstOrDefault(f => f.FieldName == field);
        if (fieldAttribute == null) return;

        fieldAttribute.Changed = fieldAttribute.Changed == false;

        fieldAttribute.Value = value;
    }

所以我喜欢这个,但它不起作用:

  foreach (var result in results)
  {
       result.SetFieldValue("payment_method_ids", _NewPaymentLine.ToArray());
       result.Save();
  }

_NewPaymentLine 是我填写的列表:

var _NewPaymentLine = new List<object>();
_NewPaymentLine.Add(new object[] { 4, Payment_Ids.GetField("id").Value });

我只能说 input valueSystem.Object[] 类型,output valueSystem.Int32[] 类型。

实际上,我 return 和 SetFieldValue 是一个 System.Object[],但我想我必须 return 一个 System.Int32[],所以问题是,我怎样才能使我的 System.Object[] 列表变成 System.Int32[]

在此先感谢,我希望有人能帮助我。

不确定这是不是你假装的,但你必须创建一个函数来将你的 Object 转换为 Int

public List<int> DoConvert(List<Object> objectsList){
    List<int> intList = new List<int>();
    foreach(var object in objectsList)
    {
        intList.Add(object.MyNumber)       //Input your logic here
    }
    return intList;
}