1

我正在使用 C# 来连接 FedEx API,但我对如何修改一些现有代码以满足我的需求感到有些困惑。包含的代码片段是他们的预制代码的一部分,他们在其中对如何使用 1 种商品进行了示例。但是,当我的代码运行时,我将拥有 n 个商品……而且我不确定如何动态解决这个问题。

例如,如果我有 3 种商品,并且我手动添加 3new Commodity()条语句,它将正常工作。但是,这需要是动态的。

request.RequestedShipment.CustomsClearanceDetail.Commodities = new Commodity[3] { 
  new Commodity(), 
  new Commodity(), 
  new Commodity() 
};

这是方法的开始,第一行是我需要帮助的地方。之后,我认为根据数组大小将其他所有内容放在一个循环中应该没问题。

request.RequestedShipment.CustomsClearanceDetail.Commodities = new Commodity[1] { 
  new Commodity() 
};

request.RequestedShipment.CustomsClearanceDetail.Commodities[0].NumberOfPieces = "1";
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].Description = "Books";
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].CountryOfManufacture = "US";
//
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].Weight = new Weight();
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].Weight.Value = 1.0M;
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].Weight.Units = WeightUnits.LB;

如果我只是这样做,我会收到一个错误:

request.RequestedShipment.CustomsClearanceDetail.Commodities = new Commodity[3] { 
  new Commodity() 
};

“预计长度为 3 的数组初始值设定项”

4

2 回答 2

4

首先,您不必提及1或根本不必提及3;放置[]并让系统为您计算所需的长度:

// Commodity[3] will be created
request.RequestedShipment.CustomsClearanceDetail.Commodities = new Commodity[] { 
  new Commodity(), 
  new Commodity(), 
  new Commodity() 
};

或者

// Commodity[1] will be created
request.RequestedShipment.CustomsClearanceDetail.Commodities = new Commodity[] { 
  new Commodity()
};

如果你想创建一个 size 数组n,你可以试试Linq

using System.Linq;

...

int n = 3;

request.RequestedShipment.CustomsClearanceDetail.Commodities = Enumerable
  .Range(0, n) 
  .Select(index => new Commodity())
  .ToArray();

甚至

int n = 3;

// 3 equivalent commodities
request.RequestedShipment.CustomsClearanceDetail.Commodities = Enumerable
  .Range(0, n) 
  .Select(index => new Commodity() {
     NumberOfPieces = "1", 
     Description = "Books",
     CountryOfManufacture = "US",
     Weight = new Weight() {
       Value = 1.0M,
       Units = WeightUnits.LB
     }
   })
  .ToArray(); 
于 2019-06-13T11:57:53.723 回答
0

@Dmitry Sir,几乎涵盖了上述答案中的所有内容......但是,下面的部分也可以写成以下部分:

您的原始代码

request.RequestedShipment.CustomsClearanceDetail.Commodities = new Commodity[1] { 
  new Commodity() 
};

request.RequestedShipment.CustomsClearanceDetail.Commodities[0].NumberOfPieces = "1";
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].Description = "Books";
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].CountryOfManufacture = "US";
//
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].Weight = new Weight();
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].Weight.Value = 1.0M;
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].Weight.Units = WeightUnits.LB;

可以修改如下:

修改动态len数组和属性初始化代码

request.RequestedShipment.CustomsClearanceDetail.Commodities = new Commodity[] { 
   new Commodity {
      NumberOfPieces = "1",
      Description = "Books",
      CountryOfManufacture = "US",
      Weight = new Weight{
            Value = 1.0M,
            Units = WeightUnits.LB
      },
      // add more properties... as per requirement
   },
   // add more objects... as per requirement
};
于 2019-06-13T12:19:35.853 回答