.NET中的注入词典 [英] Injective Dictionary in .NET

查看:65
本文介绍了.NET中的注入词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,下午或晚上,



有什么像注入 在.NET中的字典,或至少我可以强制一个字典定义一个注入映射?



非常感谢。

解决方案

p>你可以这样使用

  public class InjectiveDictionary< T,V> :词典< T,V> 
{
public new void Add(T key,V value)
{
if(this.ContainsValue(value))
{
throw new Exception (已使用价值);
}
base.Add(key,value);
}


public InjectiveDictionary(Dictionary< T,V> dict)
{
foreach(dict中的var entry)
{
this.Add(entry.Key,entry.Value);
}
}

public InjectiveDictionary()
{
}
}
pre>

Good morning, afternoon or night,

Is there anything like an injective Dictionary in .NET, or at least any way I can force a Dictionary to define an injective mapping?

Thank you very much.

解决方案

You can use it like this

    public class InjectiveDictionary<T, V> : Dictionary<T, V>
    {
        public new void Add(T key, V value)
        {
            if (this.ContainsValue(value))
            {
                throw new Exception("value already used");
            }
            base.Add(key, value);
        }


    public InjectiveDictionary(Dictionary<T, V> dict)
    {
        foreach (var entry in dict)
        {
            this.Add(entry.Key, entry.Value);
        }
    }

    public InjectiveDictionary()
    {
    }
}

这篇关于.NET中的注入词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆