Please tell me what I'm doing wrong. It is necessary to put in one dictionary various objects that implement one interface with a generic argument.
Here's the code for an example:
There is an interface and two implementations OneParameter and TwoParameter:
public interface IParameter
{
string Name { get; }
}
public class ParameterBase : IParameter
{
public ParameterBase(string name)
{
Name = name;
}
public string Name { get; }
}
public class OneParameter : ParameterBase
{
public OneParameter(int oneProperty)
: base("One")
{
OneProperty = oneProperty;
}
public int OneProperty { get; set; }
}
public class TwoParameter : ParameterBase
{
public TwoParameter(int twoProperty)
: base("Two")
{
TwoProperty = twoProperty;
}
public int TwoProperty { get; set; }
}
There are also two behavior strategies:
public interface IRequestStrategy<TRequestParameter>
where TRequestParameter : IParameter
{
Task RequestDocument(TRequestParameter parameter);
}
public class OneRequestStrategy : IRequestStrategy<OneParameter>
{
public Task RequestDocument(OneParameter parameter)
{
throw new NotImplementedException();
}
}
public class TwoRequestStrategy : IRequestStrategy<TwoParameter>
{
public Task RequestDocument(TwoParameter parameter)
{
throw new NotImplementedException();
}
}
Next, I try to register strategies in one dictionary:
private static void TestStrategies()
{
Dictionary<int, IRequestStrategy<IParameter>> _strategies =
new Dictionary<int, IRequestStrategy<IParameter>>();
var str1 = new OneRequestStrategy();
var str2 = new TwoRequestStrategy();
_strategies.Add(1, (IRequestStrategy<IParameter>)str1);
_strategies.Add(2, str2);
}
In the first case, there is an error of type casting:
Unable to cast object of type 'OneRequestStrategy' to type IRequestStrategy`1[IParameter]'.
In the second, it's the same only when compiling:
Error CS1503 : cannot convert from 'TwoRequestStrategy' to 'IRequestStrategy<IParameter>'