我对泛型的研究越来越深入,现在有一个我需要帮助的情况。我在下面的“派生”类上得到了一个编译错误,如主题标题所示。我看到很多类似这篇文章的帖子,但我看不出有什么关系。有人能告诉我如何解决这个问题吗?
using System;
using System.Collections.Generic;
namespace Example
{
public class ViewContext
{
ViewContext() { }
}
public interface IModel
{
}
public interface IView<T> where T : IModel
{
ViewContext ViewContext { get; set; }
}
public class SomeModel : IModel
{
public SomeModel() { }
public int ID { get; set; }
}
public class Base<T> where T : IModel
{
public Base(IView<T> view)
{
}
}
public class Derived<SomeModel> : Base<SomeModel> where SomeModel : IModel
{
public Derived(IView<SomeModel> view)
: base(view)
{
SomeModel m = (SomeModel)Activator.CreateInstance(typeof(SomeModel));
Service<SomeModel> s = new Service<SomeModel>();
s.Work(m);
}
}
public class Service<SomeModel> where SomeModel : IModel
{
public Service()
{
}
public void Work(SomeModel m)
{
}
}
}
https://stackoverflow.com/questions/6451120
复制相似问题