Dictionary intellisense

2019-08-25 18:39发布

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);

3条回答
做个烂人
2楼-- · 2019-08-25 18:57

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.

查看更多
We Are One
3楼-- · 2019-08-25 19:02

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.

查看更多
做个烂人
4楼-- · 2019-08-25 19:05

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.)

查看更多
登录 后发表回答