4

给定下面的类和接口,我想知道为什么隐式转换:

ISomeModelAbstract<IBasicModel> x = new ConcreteClass();

是不可能的。我试过了

public interface ISomeModelAbstract<out T> where T: IBasicModel

但后来我不能使用GetByIdGetAll方法。我感谢任何帮助或提示。谢谢你。

public interface IBasicModel {
    string _id { get; set; }
}

public class SomeModel: IBasicModel {
    public string _id { get; set; }

    /* some other properties! */
}

public interface ISomeModelAbstract<T> where T: IBasicModel
{
    bool Save(T model);
    T GetById(string id);
    IEnumerable<T> GetAll();
    bool Update(string id, T model);
    bool Delete(string id);
}

public abstract class SomeModelAbstract<T> : ISomeModelAbstract<T> where T : IBasicModel
{
    public bool Save(T model)
    {
        throw new System.NotImplementedException();
    }

    public T GetById(string id)
    {
        throw new System.NotImplementedException();
    }

    public IEnumerable<T> GetAll()
    {
        throw new System.NotImplementedException();
    }

    public bool Update(string id, T model)
    {
        throw new System.NotImplementedException();
    }

    public bool Delete(string id)
    {
        throw new System.NotImplementedException();
    }
}

public interface IConcreteClass: ISomeModelAbstract<SomeModel> { }

public class ConcreteClass: SomeModelAbstract<SomeModel>, IConcreteClass { }
4

2 回答 2

3

由于协方差问题,这不起作用。考虑这个示例代码。

public class SomeModel2: IBasicModel {
    public string _id { get; set; }

    /* some other properties! */
}

之后,您可以将 SomeModel2 的某些对象传递给 x 的 Save 方法,显然,这是不行的。

    ISomeModelAbstract<IBasicModel> x = new ConcreteClass();
    var m = new SomeModel2();
    x.Save(m);

为了防止这种情况,您应该隐含地告诉您仅在返回(输出)位置使用泛型类型,而不是在输入中。例如:

public interface ISomeModelAbstract<out T> where T: IBasicModel

不幸的是,在这样做之后,您无法在 ISomeModelAbstract 界面中使用 Save and Update 方法。因为他们在参数(输入)的地方使用了 T。

有关更多信息,请参见以下链接:http ://tomasp.net/blog/variance-explained.aspx/

于 2018-05-07T03:57:40.180 回答
1

另一个答案已经描述了它在当前状态下不起作用的原因。我想补充一点,在这种情况下,将接口(或两者)的协变或逆变部分提取到单独的接口中通常很有用。例如:

// covariant part, T is used only as return value
// ISomeModelRead is not the best name of course
public interface ISomeModelRead<out T> where T : IBasicModel {
    T GetById(string id);
    IEnumerable<T> GetAll();
}

// the rest of interface, also implementing covariant part
public interface ISomeModelAbstract<T> : ISomeModelRead<T> where T : IBasicModel  {
    bool Save(T model);
    bool Update(string id, T model);
    bool Delete(string id);
}

现在一切都一样了,除了你可以这样做:

ISomeModelRead<IBasicModel> x = new ConcreteClass();
x.GetAll();
x.GetById("id");
于 2018-05-07T05:04:53.227 回答