0

我在智能合约中有功能:

 struct DrugBox {
        uint256 weight; // weight is accumulated by delegation
        uint256 creationDate;
        address producer;
        string drugName;
        uint256 id;
    }
  function getAllBoxes() public view returns (DrugBox[] memory box1)  {
        return boxes;
    }

我有 C# 中的代码。我想从智能合约中返回药箱列表

    [FunctionOutput]
    public class DrugBoxesDTO 
    {
        [Parameter("tuple[]", "box1", 1)]
        public List<DrugBoxDTO> Boxes { get; set; }
    }

    public class DrugBoxDTO 
    {
        public string DrugName { get; set; }
        public string Producer { get; set; }
        public int Weight { get; set; } 
        public int Id { get; set; }
    }

Task<DrugBoxesDTO> qwe = drugStoreContract.GetFunction("getAllBoxes").CallAsync<DrugBoxesDTO>();

但我收到一个错误:

System.AggregateException
HResult=0x80131500
Message=One or more errors occurred. (Arrays containing Dynamic Types are not supported)

Inner Exception 1:
NotSupportedException: Arrays containing Dynamic Types are not supported

如何正确反序列化对象列表?

4

1 回答 1

0

对于DrugBoxDTO类中的字段,还需要添加Parameter属性。对于 Id & Weight 字段,使用 BigInteger 而不是 int,因为您在合同中使用 uint256。

public class DrugBoxDTO 
{
    [Parameter("uint256", "weight", 1)]
    public BigInteger Weight { get; set; }
    
    [Parameter("uint256", "creationDate", 2)]
    public BigInteger CreationDate { get; set; }
    
    [Parameter("address", "producer", 3)]
    public string Producer { get; set; }
    
    [Parameter("string", "drugName", 4)]
    public string DrugName { get; set; }
    
    [Parameter("uint256", "token", 5)]
    public BigInteger Id { get; set; }
}

建议使用 nethereum代码生成工具生成 C# 代码: https ://docs.nethereum.com/en/latest/nethereum-code-generation/

于 2021-12-30T02:08:09.543 回答