【问题标题】:Can you have an internal interface that is used internally only by public interfaces您能否拥有一个仅由公共接口在内部使用的内部接口
【发布时间】:2012-07-13 08:28:07
【问题描述】:

我想做一些类似的事情:

internal interface IInternalStore
{
    object GetValue(object key);
}

public interface IStore<in TKey, TValue>
    : IInternalStore
{
    TValue GetValue(TKey key);
}

public interface IStore
    : IInternalStore
{
    TValue GetValue<in TKey, TValue>(TKey key);
}

我想这样做的原因是我可以检查该类是否为 IInternalStore 而不必检查各个接口类型。

IE。

// Defined by the implementing developer
public class MyStoreA
    : IStore<int, int>
{
    int GetValue(int key);
}

public class MyStoreB<TKey, TValue>
    : IStore
{
    TValue GetValue(TKey key);
}

// Internal method used by me
void GetValueFromStore(object store)
{
    if (store is IInternalStore)
    {
       {do something}
    }
}

但据我所知,我想做的事情是不可能的,因为 IInternalStore 必须与继承的接口具有相同的访问器,并且需要开发人员实现所有继承的方法。

我错了吗?有没有办法做我想做的事?

【问题讨论】:

    标签: c# inheritance interface accessor


    【解决方案1】:

    我错了吗?有没有办法做我想做的事?

    不,你是对的 - 公共接口不能扩展内部接口。

    我建议你把两个接口分开。然后,您可以随时拥有一个 internal 接口来扩展两者,如果您愿意,或者只是让相关类实现这两个类。

    【讨论】:

      猜你喜欢
      • 2013-02-02
      • 2014-09-09
      • 2021-06-14
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 2011-12-03
      相关资源
      最近更新 更多