Pergunta

I want to implement a wrapper class for a Dictionary that maps a Type to a generic List of that Type. For example:

**Key**               **Value**
typeof(InterfaceA), List<InterfaceA>
typeof(InterfaceB), List<InterfaceB>
typeof(MyClass), List<MyClass>
...

I then want to interact with the wrapper class by using types.

public void NewEntry<T>()
{
    MyDict.Add(typeof(T), new List<T>());
}

public List<T> GetEntry<T>()
{
    return MyDict[typeof(T)];
}

public void RemoveEntry<T>()
{
    MyDict.Remove(typeof(T));
}

Is there any elegant way to do this?

EDIT: to clarify, the point of this is so that with

GetEntry<MyInterface>()

the items in the list are guaranteed to follow the contract of MyInterface. Each entry would have a different Type key, and each List of items would follow the contract of that Type.

Foi útil?

Solução

You could use the following static class

public static class GenericLists
{
    private static Dictionary<Type, object> MyDict = new Dictionary<Type, object>();
    public static void NewEntry<T>()
    {
        MyDict.Add(typeof(T), new List<T>());
    }

    public static List<T> GetEntry<T>()
    {
        return (List<T>)MyDict[typeof(T)];
    }

    public static void RemoveEntry<T>()
    {
        MyDict.Remove(typeof(T));
    }

}

Or you could use

public class GenericLists<T>
{
    private Dictionary<Type, List<T>> MyDict = new Dictionary<Type, List<T>>();

    public void NewEntry()
    {
        MyDict.Add(typeof(T), new List<T>());
    }

    public List<T> GetEntry()
    {
        return MyDict[typeof(T)];
    }

    public void RemoveEntry()
    {
        MyDict.Remove(typeof(T));
    }
}

if you really want to initialize it, but I think the static will work better.

Outras dicas

If you're willing to store everything statically, you can use the type system:

static class MyDict {
    private static class Data<T> {
        public static readonly List<T> items = new List<T>();
    }
    public static List<T> Get<T>() { return Data<T>.items; }
    public static void Add<T>(T item) { Data<T>.items.Add(item); }
}

Note that this makes it impossible to remove a key (you can't unload a type), although you can Clear() it.

You can do it as an instance-based class also (see below), but my preference, if it works for you, is to use a static variable in a static class as SLaks demonstrated in the "use the type system" post.

public class GenericTypeListDictionary
{
    private readonly Dictionary<Type, object> _dictionaryOfLists = new Dictionary<Type, object>();

    public List<T> NewEntry<T>()
    {
        var newList = new List<T>();
        _dictionaryOfLists.Add(typeof(T), newList);
        return newList;
    }

    public List<T> GetEntry<T>()
    {
        object value;

        if (_dictionaryOfLists.TryGetValue(typeof(T), out value))
        {
            return (List<T>)value;
        }

        return null;
    }

    public void RemoveEntry<T>()
    {
        _dictionaryOfLists.Remove(typeof(T));
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top