we've reached a point where we have no clue how to continue:
SHORT: We have a generic interface and a collection of the generic interface. Trying to add an implementation of the generic interface to the collection fails. What happens is that I get a compile time exception saying:
cannot convert from TestApp.IState<T>' to TestApp.IState<TestApp.IView>'
LONG [Code example]:
class Program
{
static void Main(string[] args)
{
var coll = new StateCollection();
var state = new SomeState();
coll.AddState(state);
}
}
public class StateCollection
{
private List<StateBase<IView>> _states = new List<StateBase<IView>>();
public void AddState<T>(StateBase<T> state) where T: IView
{
_states.Add(state);
}
}
public class SomeState : StateBase<SomeView>
{
public IView View
{
get;
}
}
public class SomeView : IView
{
}
public abstract class StateBase<T> where T : IView
{
private SomeView _view;
public SomeView View
{
get { return _view; }
}
}
public interface IView
{
}
Why does this happen? In the AddState we mention that T has to be an instance of IState. Could someone help us out with why this happens and how to do what we want to do?
EDIT1: We also tried:
public void AddState(IState<IView> state)
{
_states.Add(state);
}
But that just moves the compile time error to 'coll.AddState(state)' So the same thing happens in another place.
EDIT2: PROBLEM! I didn't give the right example. Out IState is not an interface but an abstract class. Very sorry for that! Changed code to use abstract class
IState<SomeView>is notIState<IView>>