0

I want to build set of methods:

I have this abstract:

public abstract Filter<T> Create(T type, string value);
public abstract IEnumerable<Filter<T>> Create(T type, IEnumerable<object> values);

And implementation:

public override Filter<DiscountFilterType> Create(DiscountFilterType type, string value)
            => new DiscountFilter {Type = type, Value = string.IsNullOrWhiteSpace(value) ? null : value};
public override IEnumerable<Filter<DiscountFilterType>> Create(DiscountFilterType type, IEnumerable<object> values)
            => values.Select(value => this.Create(type, value?.ToString())).ToList();

It looks good but if I want to do something like that:

var type = DiscountFilterType.CampaignStatus;
var values = new List<CampaignStatus>{
CampaignStatus.Active,
CampaignStatus.Accepted,
CampaignStatus.Supplement};
// Act
var result = sut.Create(ype, values);

And it trying to send a array to string method.

'CS1503 Argument 2: cannot convert from 'System.Collections.Generic.List<CampaignStatus>' to 'string'

What is going on? And if I want to do it this way:

    public abstract Filter<T> Create(T type, object value);
    public abstract IEnumerable<Filter<T>> Create(T type, IEnumerable<object> values);

How?

@Edit: I want to have method to create filter which takes: 1. one type, and one value 2. one type, multiple values and returns multiple filters 3. multiple types and multiple values and returns multiple filters

Type is always some of enum and value in the end should be as string but I don't want to cast values/arrays ToString before calling the method.

Nerf
  • 938
  • 1
  • 13
  • 30

1 Answers1

1

You need an additional generic parameter for the type of the values in the enumerable sequence (instead of using object). Try the following definitions for the abstract types:

public abstract Filter<TType, TValue> Create(TType type, TValue value);
public abstract IEnumerable<Filter<TType, TValue>> Create(TType type, IEnumerable<TValue> values);

Then you can convert the instances of the type TValue to strings in the method body (using ToString if that is what your convention requires, this part is a bit outside of the scope of your question).

odyss-jii
  • 2,619
  • 15
  • 21