简体   繁体   English

类继承了通用字典 <string, IFoo> 和界面

[英]Class inherits generic dictionary<string, IFoo> and Interface

I have a class that inherits a generic dictionary and an inteface 我有一个继承通用字典和接口的类

public class MyDictionary: Dictionary<string, IFoo>, IMyDictionary
{
}

the issue is that consumers of this class are looking for the '.Keys' and ".Values" properties of the interface so i added: 问题是这个类的消费者正在寻找界面的'.Keys'和“.Values”属性,所以我补充说:

    /// <summary>
    /// 
    /// </summary>
    ICollection<string> Keys { get; }

    /// <summary>
    /// 
    /// </summary>
    IEnumerable<IFoo> Values { get; }

to the interface. 到界面。

Now, the implementation needs to have this as well but when i implement these, i get this error: 现在,实现需要也有这个,但是当我实现这些时,我得到这个错误:

"The keyword new is required because it hides property Keys . .. " “关键字new是必需的,因为它隐藏了属性键......”

so what do i need to do. 所以我需要做什么。 Should i be adding a "new" in front of these get properties? 我应该在这些获取属性前添加“新”吗?

Another option would be to change the types on the interface to be: 另一种选择是将接口上的类型更改为:

public interface IMyDictionary
{
    /// <summary>
    /// 
    /// </summary>
    Dictionary<string, IFoo>.KeyCollection Keys { get; }

    /// <summary>
    /// 
    /// </summary>
    Dictionary<string, IFoo>.ValueCollection Values { get; }
}

That way the interface is already implemented by the dictionary saving you the trouble of implementing the properties again, and it doesn't hide or cover the original implementation. 这样,接口已经由字典实现,省去了再次实现属性的麻烦,并且它不会隐藏或覆盖原始实现。

Dictionary<string, IFoo> implements the IDictionary<TKey,TValue> interface which already provides the Keys and Values properties. Dictionary <string,IFoo>实现IDictionary <TKey,TValue>接口,该接口已提供Keys和Values属性。 There shouldn't be a need to create your own properties, but the way to get around the compiler warning is to add the new keyword at the beginning of your property declarations in your class. 不需要创建自己的属性,但绕过编译器警告的方法是在类的属性声明的开头添加new关键字。

The reason is that your keys and values properties are hiding the implementation of the keys and values properties in the Dictionary class. 原因是您的键和值属性隐藏了Dictionary类中键和值属性的实现。

EDIT: Yes all you have to do is add the new keyword into you property. 编辑:是的,您所要做的就是将新关键字添加到您的属性中。 So you code will look somthing like this: 所以你的代码看起来像这样:

class IFoo
{ }

interface MyDictionary
{
    ICollection<string> Keys { get; }

    /// <summary>
    /// 
    /// </summary>
    IEnumerable<IFoo> Values { get; }
}

class IStuff : Dictionary<string, IFoo>, MyDictionary
{
    #region MyDictionary Members

    //Note the new keyword.
    public new ICollection<string> Keys
    {
        get { throw new NotImplementedException(); }
    }

    public new IEnumerable<IFoo> Values
    {
        get { throw new NotImplementedException(); }
    }

    #endregion
}

I would still recommend implementing IDictionary though. 我仍然建议实现IDictionary。

You could also use explicit interface implementation. 您还可以使用显式接口实现。 That way if someone is referencing MyDictionary by using the interface IMyDictionary they will see the methods and nothing else. 这样,如果有人通过使用IMyDictionary接口引用MyDictionary,他们将看到方法,而不是其他任何东西。 I would also recommend implementing IDictionary instead. 我还建议改为实现IDictionary。 Unless you want to restrict usage to just Keys and Values. 除非您想将使用限制为仅键和值。

public class MyDictionary : Dictionary<string, IFoo>, IMyDictionary
{
    ICollection<string> IMyDictionary.Keys
    {
        get { return Keys; }
    }

    IEnumerable<IFoo> IMyDictionary.Values
    {
        get { return Values; }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM