创建独特词典的集合

时间:2019-01-08 21:01:02

标签: c#

我想创建多个字典,这些字典以Guid作为键,x作为值,其中x必须实现接口IComponent

所有这些字典都应存储在一个集合中,当添加componentType x的字典但该字典已经存在时,该集合将引发异常。

我不确定是否必须自己创建此字典和集合,或者是否可以使用某些内容。因此字典本身就是

MyDict<T> : Dictionary<Guid, T> where T : IComponent

但是我想我可以用KeyedByTypeCollection解决这个问题。

    private KeyedByTypeCollection<Dictionary<Guid, IComponent>> componentPools = new KeyedByTypeCollection<Dictionary<Guid, IComponent>>();

    public Dictionary<Guid, T> GetComponentPool<T>() where T : IComponent
    {
        return componentPools[typeof(T)]; // not working
    }

    public void AddComponentPool<T>() where T : IComponent
    {
        componentPools.Add(new Dictionary<Guid, T>()); // not working
        // other stuff
    }

    public void RemoveComponentPool<T>() where T : IComponent
    {
        componentPools.Remove(typeof(T)); // this works
        // other stuff
    }

这个例子有两个问题

  • GetComponentPool:无法将IComponent隐式转换为T
  • AddComponentPool:无法从T转换为IComponent

是否可以修复代码?还是无法使用KeyedByTypeCollection<Dictionary<Guid, IComponent>>

1 个答案:

答案 0 :(得分:0)

尝试

         public class MyDictionary<TKey, TValue> : Dictionary<TKey, TValue>
            {
                public Type ContainingType { get; set; }
            }

          public class MyStrangerList
            {
                private readonly List<MyDictionary<Guid, IComponent>> _componentPools = new List<MyDictionary<Guid, IComponent>>();

                public MyDictionary<Guid, IComponent> GetComponentPool<T>() where T : IComponent
                {
                    return _componentPools.FirstOrDefault(x => x.ContainingType == typeof(T));
                }

                public void AddComponentPool<T>() where T : IComponent
                {
                    var currentPool = GetComponentPool<T>();
                    if (currentPool == null)
                    {
                        var mydictionary = new MyDictionary<Guid, IComponent> { ContainingType = typeof(T) };
                        _componentPools.Add(mydictionary);
                    }
                }

                public void RemoveComponentPool<T>() where T : IComponent
                {
                    var currentPool = GetComponentPool<T>();
                    if (currentPool != null)
                    {
                        _componentPools.Remove(currentPool);
                    }
                }
            }


    private void Usage()
    {
        var myStrangerList = new MyStrangerList();

        myStrangerList.AddComponentPool<TextBox>(); //add
        myStrangerList.AddComponentPool<ComboBox>(); //add
        myStrangerList.AddComponentPool<ListBox>(); //add
        myStrangerList.AddComponentPool<CheckBox>(); //add
        myStrangerList.AddComponentPool<TextBox>(); //duplicate not added
        myStrangerList.RemoveComponentPool<ListBox>(); //remove

        var textBoxPool = myStrangerList.GetComponentPool<TextBox>();
        if (textBoxPool != null)
        {
            var textBox1 = new TextBox();
            var textBox2 = new TextBox();
            var textBox3 = new TextBox();
            textBoxPool.Add(Guid.NewGuid(), textBox1);
            textBoxPool.Add(Guid.NewGuid(), textBox2);
            textBoxPool.Add(Guid.NewGuid(), textBox3);
        }
    }