我真的不明白这个共同/逆转的事情......我不能同时拥有通用的get和set方法?

sim*_*sjo 9 inheritance interface covariance contravariance c#-4.0

我想我会用一些例子解释我的问题..

interface IModel {}

class MyModel : IModel {}

interface IRepo<T> where T: IModel {
}

class Repo : IRepo<MyModel> {
}

// Cannot implicitly convert.. An explicit convertion exists. Missing cast?
IRepo<IModel> repo = new Repo();
Run Code Online (Sandbox Code Playgroud)

所以我需要协方差..

interface IRepo<out T> where T: IModel {
}
Run Code Online (Sandbox Code Playgroud)

很好,它的工作原理.然后我想用它:

interface IRepo<out T> where T: IModel {
    T ReturnSomething();
}

class Repo : IRepo<MyModel> {
    public MyModel ReturnSomething() { return default(MyModel); }
}
Run Code Online (Sandbox Code Playgroud)

一切都很好,但回购也需要插入对象.使用out参数,我们不能这样做:

// Invalid variance: The type parameter 'T' must be contravariantly valid on 'IRepo<T>.InsertSomething(T)'. 'T' is covariant.
interface IRepo<out T> where T: IModel {
    T ReturnSomething();
    void InsertSomething(T thing);
}

class Repo : IRepo<MyModel> {
    public MyModel ReturnSomething() { return default(MyModel); }
    public void InsertSomething(MyModel thing) { }
}
Run Code Online (Sandbox Code Playgroud)

所以我尝试添加两个参数:

interface IRepo<out TReturn, TInsert>
    where TReturn : IModel
    where TInsert : IModel
{
    TReturn ReturnSomething();
    void InsertSomething(TInsert thing);
}
Run Code Online (Sandbox Code Playgroud)

我得到了与第一个例子中相同的错误.我在使用时遇到同样的错误in TInsert

那么我怎么能支持插入和取出?

编辑:所以我找到了一个可能的解决方案,但它远非最佳

interface IRepo<out TResult> where TResult : IModel {
    TResult ReturnSomething();
    // I need to duplicate my constraint here..
    void InsertSomething<TInsert>(TInsert thing) where TInsert : IModel;
}


class Repo : IRepo<MyModel> {
    public MyModel ReturnSomething() { return default(MyModel); }
    // ... And here
    public void InsertSomething<T>(T thing) where T: IModel { }
}
Run Code Online (Sandbox Code Playgroud)

EDIT2:回应Eric:这是我想要实现的更完整的例子.我真的想要协方差,所以我可以对IRepo实例进行分组,我仍然希望它们使用模型作为实例添加/更新方法.我知道我无法获得编译时类型安全性来添加项目,但对于这个用例,我只需要阅读元素.

interface IModel { }
class SomeModel : IModel { }
class OtherModel : IModel { }

interface IRepo<T>
{
    T ReturnSomething();
    void AddSomething(T thing);
}

interface ISubRepo<T> : IRepo<T> where T : IModel { }

class SomeSubRepo : ISubRepo<SomeModel> {
    public SomeModel ReturnSomething() { return default(SomeModel); }
    public void AddSomething(SomeModel thing) { }
}

class OtherSubRepo : ISubRepo<OtherModel> {
    public OtherModel ReturnSomething() { return default(OtherModel); }
    public void AddSomething(OtherModel thing) { }
}

class Program {
    static void Main(string[] args)
    {
        ISubRepo<IModel>[] everyone = new ISubRepo<IModel>[] {
            new SomeSubRepo(),
            new OtherSubRepo() 
        };

        WorkOnAll(everyone);
    }

    static void WorkOnAll(IEnumerable<ISubRepo<IModel>> everyone)
    {
        foreach(ISubRepo<IModel> repo in everyone) {
            IModel model = repo.ReturnSomething();
            // Etc.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

kvb*_*kvb 2

我认为最好的选择是将界面一分为二:

    interface IReadableRepo<out T> where T : IModel
    {
        T ReturnSomething();
    }

    interface IWritableRepo<in T> where T : IModel
    {
        void InsertSomething(T thing);
    }

    class Repo : IReadableRepo<MyModel>, IWritableRepo<MyModel>
    {
        ...
    }
Run Code Online (Sandbox Code Playgroud)

现在您可以创建一个List<IReadableRepo<IModel>>包含Repo实例的对象。