The error occurs because your NodeViewModel<T>
class is not covariant. Whether you can do this depends on the operations you need to support, but for the code in your question you can create an interface:
public interface INodeViewModel<out T> where T : Node
{
T Data { get; }
}
then implement it
public NodeViewModel<T> : INodeViewModel<T> where T : Node { ... }
then change the type of the items in l
:
List<INodeViewModel<Node>> l = new List<INodeViewModel<Node>>();