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.

6
  • What @DavidG said. On top of that, your generic approach seems slightly peculiar. Maybe take a step back and describe what you intend to do? Commented Apr 13, 2017 at 14:04
  • English only please Commented Apr 13, 2017 at 14:13
  • 1
    stackoverflow.com/questions/2873480/… Commented Apr 13, 2017 at 14:14
  • 2
    Your List<CampaignStatus> is not assignable to IEnumerable<object>, so the compiler is falling back to the T, string overload. Commented Apr 13, 2017 at 14:14
  • The quick fix is to change your method signature to Create(DiscountFilterType type, IEnumerable<T> values) Commented Apr 13, 2017 at 14:15

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

1 Comment

But TValue can be string, custom class, datetime and int.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.