Dictionary intellisense

2019-08-25 18:21发布

问题:

Is there a way to get intellisense for all the keys in a Dictionary<string,string> in C#?

Or is it possible to get intellisense for a method with a string parameter.

I want to have something like ResourceManager.GetString("") has. and if possible i don't want to create a static class with constant strings to simulate it.

Edit:

I am currently using a dictionary to combine multiple resource files in to one dictionary, this is done becouse resources are used in multiple projects and need to be overwriten.

var temp = ResourceFile1.ResourceManager
                    .GetResourceSet(CultureInfo.CurrentUICulture, true, true)
                    .Cast<object>()
                    .Select(
                        x => new
                            {
                                ID = 1,
                                Value = x.GetType().GetProperty("Value").GetValue(x, null).ToString(),
                                Key = x.GetType().GetProperty("Key").GetValue(x, null).ToString()
                            });
                temp = temp.Concat(
                    ResourceFile2.ResourceManager
                        .GetResourceSet(CultureInfo.CurrentUICulture, true, true)
                        .Cast<object>()
                        .Select(
                            x => new
                            {
                                ID = 2,
                                Value = x.GetType().GetProperty("Value").GetValue(x, null).ToString(),
                                Key = x.GetType().GetProperty("Key").GetValue(x, null).ToString()
                            }));
                temp = temp.Concat(
                    ResourceFile3.ResourceManager
                        .GetResourceSet(CultureInfo.CurrentUICulture, true, true)
                        .Cast<object>()
                        .Select(
                            x => new
                                {
                                    ID = 3,
                                    Value = x.GetType().GetProperty("Value").GetValue(x, null).ToString(),
                                    Key = x.GetType().GetProperty("Key").GetValue(x, null).ToString()
                                }));
            ResourceDictionary = temp.GroupBy(x => x.Key)
                .Select(x => x.FirstOrDefault(c => c.ID == x.Max(v => v.ID)))
                .ToDictionary(x => x.Key, x => x.Value);

回答1:

Don't use string, but an enum in this case, if you know the finite values.

By definition, string can contains any value at run-time, while enums are known at compile time.

Or you can simply avoid the use of a dictionary. If you know that your dictionary will contains always the same keys, you should better create a class with one property per key you previously used.



回答2:

No, there is no way to have Intellisense over something that is not known at compile time such as the keys in a dictionary. Intellisense works only for compile-time objects.



回答3:

You can't use Dictionary since it uses the hash function. Since similiar strings don't have similiar hash, this simply does not fit for your purpose.

If you need to implement this yourself I think you could solve it by building a radix tree.

(I interpreted it as if you wanted intellisense at runtime.)