代码之家  ›  专栏  ›  技术社区  ›  SkyeBoniwell

向API调用添加并填充IEnumerable属性

  •  0
  • SkyeBoniwell  · 技术社区  · 3 年前

        AutoMaximizer.AuthUser yourCredentials = new AutoMaximizer.AuthUser
        {
            UserKey = "x1213"
        };
    
        AutoMaximizer.Pagers availableCars = new AutoMaximizer.Pagers
        {
            TotalCalls = 65,
            Router = 91220
        };
    
        ISessionManagement s = new ManagementClient();
    
        FactoryResponse response;
    
        response = await s.GetAll(yourCredentials, new ListRequest
        {
            Pagers = availableCars,
            SortIncreasing = true
        });
    

    它正在工作,但我想在发出请求时添加另一个属性。

    该属性称为 Types 而且是一个 IEnumerable<Type>

    Types   =  Gets or sets an enumeration of types to include in the response.
    

    在API参考中,我发现:

    public enum Type : int
    {
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        Auto = 0,
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        Truck = 1,
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        Motorcycle = 2
        
    }
    

    GetAll 方法。

    List<AutoMaximizer.Type> types = new List<AutoMaximizer.Type>();
    types.Add(AutoMaximizer.Type.Auto);
    types.Add(AutoMaximizer.Type.Truck);
    types.Add(AutoMaximizer.Type.Motorcycle);
    

    response = await s.GetAll(yourCredentials, new ListRequest
    {
        Pagers = availableCars,
        SortIncreasing = true,
        Types = types
    });
    

    但这给了我一个错误:

    Cannot implicitly convert type Systems.Collections.Generic.List<AutoMaximizer.Type> to AutoMaximizer.Type[]
    

    我不知道现在该怎么办。。。

    谢谢

    2 回复  |  直到 3 年前
        1
  •  2
  •   Blindy    3 年前

    读取您的错误,它需要的是数组,而不是列表:

    response = await s.GetAll(yourCredentials, new ListRequest
    {
        Pagers = availableCars,
        SortIncreasing = true,
        Types = new[] { Type.Auto, Type.Truck, Type.Motorcycle },
    });
    
        2
  •  2
  •   David    3 年前

    根据错误,该 ListRequest

    response = await s.GetAll(yourCredentials, new ListRequest
    {
        Pagers = availableCars,
        SortIncreasing = true,
        Types = types.ToArray()
    });
    

    或者仅使用数组开始:

    AutoMaximizer.Type[] types = new [] { AutoMaximizer.Type.Auto, AutoMaximizer.Type.Truck, AutoMaximizer.Type.Motorcycle };