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

问题描述

我是 C# 新手

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

实际上我不知道如何将列表作为数组作为参数返回。 此处的此函数将使用 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[] 类型。

实际上,我用 SetFieldValue 返回的是 System.Object[],但我想我必须返回 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;
}