Can I Create a Dictionary of Generic Types?

前端 未结 10 2426
误落风尘
误落风尘 2020-11-30 02:53

I\'d like to create a Dictionary object, with string Keys, holding values which are of a generic type. I imagine that it would look something like this:

Dict         


        
相关标签:
10条回答
  • 2020-11-30 03:18

    Other posibility it's to use the variable dynamic.

    For example:

    Dictionary<string, List<dynamic>> d = new Dictionary<string, List<dynamic>>(); d.Add("Key", new List<dynamic>());

    the variable dynamic resolve the type on runtime.

    0 讨论(0)
  • 2020-11-30 03:21

    How about Dictionary<string, dynamic>? (assuming you're on C# 4)

    Dictionary<string, dynamic> Dict = new Dictionary<string, dynamic>();
    

    Source: https://stackoverflow.com/a/5038029/3270733

    0 讨论(0)
  • 2020-11-30 03:23

    EDIT: Now I've reread the question...

    You can't do this, but a custom collection would handle it to some extent. You'd basically have a generic Add method:

    public void Add<T>(string key, List<T> list)
    

    (The collection itself wouldn't be generic - unless you wanted to make the key type generic.)

    You couldn't extract values from it in a strongly typed manner though, because the compiler won't know which type you've used for a particular key. If you make the key the type itself, you end with a slightly better situation, but one which still isn't supported by the existing collections. That's the situation my original answer was responding to.

    EDIT: Original answer, when I hadn't quite read the question correctly, but which may be informative anyway...

    No, you can't make one type argument depend on another, I'm afraid. It's just one of the things one might want to express in a generic type system but which .NET's constraints don't allow for. There are always going to be such problems, and the .NET designers chose to keep generics relatively simple.

    However, you can write a collection to enforce it fairly easily. I have an example in a blog post which only keeps a single value, but it would be easy to extend that to use a list.

    0 讨论(0)
  • 2020-11-30 03:26

    No, but you can use object instead of generic type.

    Long answer: The current version of C# will not allow you to make entries of generic type in a dictionary. Your options are either a) create a custom class that is the same as a dictionary except allow it to accept generic types, or b) make your Dictionary take values of type object. I find option b to be the simpler approach.

    If you send lists of specific types, then when you go to process the lists you will have to test to see what kind of list it is. A better approach is to create lists of objects; this way you can enter integers, strings, or whatever data type you want and you don't necessarily have to test to see what type of object the List holds. This would (presumably) produce the effect you're looking for.

    Here is a short console program that does the trick:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace dictionary
    {
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            var lstIntList = new List<object>();
            var lstStrings = new List<object>();
            var lstObjects = new List<object>();
            string s = "";
    
            lstIntList.Add(1);
            lstIntList.Add(2);
            lstIntList.Add(3);
    
            lstStrings.Add("a");
            lstStrings.Add("b");
            lstStrings.Add("c");
    
            dic.Add("Numbers", lstIntList);
            dic.Add("Letters", lstStrings);
    
            foreach (KeyValuePair<string, object> kvp in dic)
            {
                Console.WriteLine("{0}", kvp.Key);
                lstObjects = ((IEnumerable)kvp.Value).Cast<object>().ToList();
    
                foreach (var obj in lstObjects)
                   {s = obj.ToString(); Console.WriteLine(s);}
                Console.WriteLine("");
            }
    
    
            Console.WriteLine("");
            Console.WriteLine("press any key to exit");
            Console.ReadKey();
        }//end main
    }
    }
    

    0 讨论(0)
  • 2020-11-30 03:30

    Would something like this work?

    public class GenericDictionary
    {
        private Dictionary<string, object> _dict = new Dictionary<string, object>();
    
        public void Add<T>(string key, T value) where T : class
        {
            _dict.Add(key, value);
        }
    
        public T GetValue<T>(string key) where T : class
        {
            return _dict[key] as T;
        }
    }
    

    Basically it wraps all the casting behind the scenes for you.

    0 讨论(0)
  • 2020-11-30 03:31

    We're using lots of reflection to create an extensible administration tool. We needed a way to register items in the global search in the module definition. Each search would return results in a consistent way, but each one had different dependencies. Here's an example of us registering search for a single module:

    public void ConfigureSearch(ISearchConfiguration config)
        {
            config.AddGlobalSearchCallback<IEmploymentDataContext>((query, ctx) =>
            {
                return ctx.Positions.Where(p => p.Name.Contains(query)).ToList().Select(p =>
                    new SearchResult("Positions", p.Name, p.ThumbnailUrl,
                        new UrlContext("edit", "position", new RouteValueDictionary(new { Id = p.Id }))
                        ));
            });
        }
    

    In the background during module registration, we iterate over every module and add the Func to a SearchTable with an instance of:

    public class GenericFuncCollection : IEnumerable<Tuple<Type, Type, Object>>
    {
        private List<Tuple<Type, Type, Object>> objects = new List<Tuple<Type, Type, Object>>();
    
        /// <summary>
        /// Stores a list of Func of T where T is unknown at compile time.
        /// </summary>
        /// <typeparam name="T1">Type of T</typeparam>
        /// <typeparam name="T2">Type of the Func</typeparam>
        /// <param name="func">Instance of the Func</param>
        public void Add<T1, T2>(Object func)
        {
            objects.Add(new Tuple<Type, Type, Object>(typeof(T1), typeof(T2), func));
        }
    
        public IEnumerator<Tuple<Type, Type, object>> GetEnumerator()
        {
            return objects.GetEnumerator();
        }
    
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return objects.GetEnumerator();
        }
    }
    

    Then when we finally call it, we do it with reflection:

    var dependency = DependencyResolver.Current.GetService(search.Item1);
    var methodInfo = search.Item2.GetMethod("Invoke");
    return (IEnumerable<SearchResult>)methodInfo.Invoke(search.Item3, new Object[] { query, dependency });
    
    0 讨论(0)
提交回复
热议问题