Reputation: 543
Generics method call - There is no implicit reference conversion from 'IValue' to 'IValueFox'. What's the deal with this code?
public interface IValueFox : IValue
{
...
}
private Mock<I> GenerateMockValue<I, T>(State state, T type)
where I : class, IValueFox
{
...
}
private void TestFoxValues()
{
var newFox = this.GenerateMockValue<IValue, object>(State.Red, null);
...
}
And getting the following error when I call 'GenerateMockValue' in method 'TestFoxValues':
'IValue' cannot be used as type parameter 'I' in the generic type or method 'GenerateMockValue'. There is no implicit reference conversion from 'IValue' to 'IValueFox'.
But what I don't really understand is, why any conversion is needed since IValueFox implements IValue. Can anyone clarify this question for me?
Thanks in advance!
Upvotes: 0
Views: 455
Reputation: 6060
Because you require a IValueFox and that is like a superset of IValue. (Every IValueFox is an IValue, but not every IValue is an IValueFox)
Therefore, IValue cannot be converted into IValueFox.
The where-clause says "I need an IValueFox", but in the TestFoxValues() method you say "I'm just gonna provide an IValue."
Here is en even simpler example: Your class requires a car. A car is a wheeled motor vehicle. Then further down you say "here, I provide you with a wheeled motor vehicle, but I'm not telling you exactly what it is. Could be a motorcycle" Then you class says "I explicitly requested a car!" <== This is the error you are getting.
Upvotes: 3