字典<串,串>智能感知 [英] Dictionary<string,string> intellisense

查看:128
本文介绍了字典<串,串>智能感知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在C#中的 Dictionary< string,string> 中获取所有密钥的智能感知?



我想要有一个类似于 ResourceManager.GetString()的方法来获取intellisense。 )有。如果可能,我不想创建一个具有常量字符串的静态类来模拟它。



编辑:



我目前正在使用一个字典将多个资源文件合并到一个字典中,这是由于在多个项目中使用的资源需要覆盖。

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


解决方案

不要使用字符串,而是在这个



根据定义,字符串可以在运行时包含任何值,而枚举在编译时已知。



或者你可以简单地避免使用字典。如果你知道你的字典总是包含相同的键,你应该更好地创建一个具有以前使用的每个键的一个属性的类。


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

解决方案

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.

这篇关于字典&LT;串,串&GT;智能感知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆