简体   繁体   English

c#设置为对象的各种类型的列表

[英]c# List of varying type set as object

I've created a generic search parameter class for reuse in several libraries (see below). 我创建了一个通用搜索参数类,以在多个库中重用(请参见下文)。 I'm trying to find a better way to implement the list of values for each parameter. 我正在尝试找到一种更好的方法来实现每个参数的值列表。 I will be passing a list of the parameters to a method but varying types for the values for each parameter. 我将参数列表传递给方法,但是每个参数的值的类型都不同。 I've implemented it as simply type object and will convert to the correct type as necessary in my code, but I feel like there is probably a better way to do this and I'm not coming up with much. 我已经将其实现为简单的类型对象,并将在代码中根据需要将其转换为正确的类型,但是我觉得可能有更好的方法可以做到这一点,而且我没有太多建议。 Anyone done anything similar or have a suggestion? 任何人都做过类似的事情或有建议吗? Thanks! 谢谢!

Generic Abstract Class: 通用抽象类:

public class SearchParameter<T>
{
    public T Name { get; set; }
    public List<object> Values { get; set; }
}

Inheriting Class: 继承类:

public enum OrderSearchParameterNames
{
    Barcode,
    DateCompleted,
    DatePlaced,
    OrderStatus,
    UserId
}

public class OrderSearchParameter : SearchParameter<OrderSearchParameterNames>
{
    public OrderSearchParameter(OrderSearchParameterNames name, List<object> values)
    {
        Name = name;
        Values = values;
    }
}

Example of method using the inherited class: 使用继承的类的方法的示例:

public ApiResponse<OrderWellplate, ApiResponseStatus> SearchOrders(int currentPageIndex, int pageSize, List<OrderSearchParameter> searchParameters, OrderSortParameter sortParameter, out int recordCount)

If the type of the list is the same as the type of Name 如果列表的类型与Name的类型相同

public class SearchParameter<T>
{
    public T Name { get; set; }
    public List<T> Values { get; set; }
    public SearchParameter(T name, List<T> values)
    {
        Name = name;
        Values = values;
    }
}

If the type of the list is different from the type of Name 如果列表的类型与Name的类型不同

public class SearchParameter<TName, TValues>
{
    public TName Name { get; set; }
    public List<TValues> { get; set; }

    public SearchParameter(TName name, List<TValues> values)
    {
        Name = name;
        Values = values;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM