简体   繁体   English

在抽象字典中使用Enum Bastetype作为Key

[英]Use a Enum Bastetype as Key in a abstract Dictionary

First I want to say that I am quite new to C# sharp. 首先,我想说我对C#sharp很新。 Is there any way implement a construct like the following? 有没有办法实现如下构造? Or is there any other way to archive this? 或者有其他方式存档吗?

public interface IClass 
{ 
    Dictionary<Enum, ISecondClass> { get; } 
}

public abstract class ClassBase : IClass
{
    public abstract Dictionary<Enum, ISecondClass> { get; protected set;}
}

public class ConcreteClass : ClassBase
{
    public override Dictionary<ConreteEnum, ISecondClass> { get; protected set;}
}

EDIT: I forgot to say that the concrete instance of the Dictionary needs to implement a custom Enum-comparer that need a concrete enum to get initialized 编辑: 我忘了说Dictionary的具体实例需要实现一个自定义的Enum-comparer,需要一个具体的枚举来初始化

For all who are interested in the custom enum-comparer, which is needed in this case, take a look at this link: Custom-Enum-Comparer 对于所有对自定义枚举比较器感兴趣的人,请查看此链接: Custom-Enum-Comparer

You can add a generic type argument to the class/interface level 您可以在类/接口级别添加泛型类型参数

public interface IClass<TEnum>
{ 
    Dictionary<TEnum, ISecondClass> { get; } 
}

public abstract class ClassBase<TEnum> : IClass<TEnum>
{
    public abstract Dictionary<TEnum, ISecondClass> { get; protected set;}
}

public class ConcreteClass : ClassBase<ConcreteEnum>
{
    public override Dictionary<ConcreteEnum, ISecondClass> { get; protected set;}
}

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

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