2012-02-09 49 views
35

现在我再解释一下我对字典的渴望!从我的previous question回答我的脑海里出现了这个问题!如何将类转换为Dictionary <string,string>?

现在的实际问题是我可以将Class转换成Dictionary吗?

在字典我想我的类属性为KEY和值特定财产作为VALUE

假设我的课是

public class Location 
{ 
    public string city { get; set; } 
    public string state { get; set; } 
    public string country { get; set; 
} 

现在假设我的数据是

city = Delhi 
state = Delhi 
country = India 

现在你可以轻松理解我的观点了!

我想制作字典!该字典应该像

我可以得到的价值!但是,我怎样才能得到属性名称(不值)?

我该怎么编码来创建它动态!这应该适用于我想要的每个班级?

你可以理解这个问题作为

我怎样才能从特定的类属性的列表?

+0

您可以尝试重新提问吗?我没有真正想到你想要达到的目标。 – ilivewithian 2012-02-09 12:00:56

+0

Insort!我想要所有变量的特定类的名字!这可能吗 ? – Chintan 2012-02-09 12:02:25

+0

@NoOne我已经回答了您,但您需要了解更多关于面向对象的内容。你在谈论_properties_。类级别的“变量”称为_fields_。通过名称来称呼所有事情是很重要的,因为理解你比以前更容易! ;) – 2012-02-09 12:06:13

回答

88

这是配方:1反射,1 LINQ到对象!

someObject.GetType() 
    .GetProperties(BindingFlags.Instance | BindingFlags.Public) 
      .ToDictionary(prop => prop.Name, prop => prop.GetValue(someObject, null)) 

自从我发表这个答案我已经签很多人发现它是有用的。我邀请所有寻找这个简单解决方案的人来检查另一个Q & A,我将其推广到一个扩展方法:Mapping object to dictionary and vice versa

+0

如果我看到像“GenericEqualityComparer”这样的“内部”属性,而不是我明确声明的属性,该怎么办?我试过'BindingFlags.DeclaredOnly'。 – drzaus 2012-07-17 15:43:29

+0

**更新**从来没有 - 我发现使用这种反射(本身在一个对象扩展名)的方法最终调用自己的转换结果,所以我的扩展名被调用的对象,然后在生成的字典上。 – drzaus 2012-07-17 15:54:41

+0

我不能在Unity3D中编译这个,我是否缺少程序集引用? – c0d3Junk13 2013-12-10 19:10:40

13

这里与反思的例子不LINQ:

Location local = new Location(); 
    local.city = "Lisbon"; 
    local.country = "Portugal"; 
    local.state = "None"; 

    PropertyInfo[] infos = local.GetType().GetProperties(); 

    Dictionary<string,string> dix = new Dictionary<string,string>(); 

    foreach (PropertyInfo info in infos) 
    { 
     dix.Add(info.Name, info.GetValue(local, null).ToString()); 
    } 

    foreach (string key in dix.Keys) 
    { 
     Console.WriteLine("nameProperty: {0}; value: {1}", key, dix[key]); 
    } 

    Console.Read(); 
+0

这节省了我至少一天的工作时间。非常感谢,如果可以的话,我会投你两次! – Kibitz503 2012-11-29 21:54:10

0
protected string getExamTimeBlock(object dataItem) 
{ 
    var dt = ((System.Collections.Specialized.StringDictionary)(dataItem)); 

    if (SPContext.Current.Web.CurrencyLocaleID == 1033) return dt["en"]; 
    else return dt["sv"]; 
} 
0

试试这个。

public static Dictionary<string, object> ObjectToDictionary(object obj) 
    { 
     Dictionary<string, object> ret = new Dictionary<string, object>(); 

     foreach (PropertyInfo prop in obj.GetType().GetProperties()) 
     { 
      string propName = prop.Name; 
      var val = obj.GetType().GetProperty(propName).GetValue(obj, null); 
      if (val != null) 
      { 
       ret.Add(propName, val.ToString()); 
      } 
      else 
      { 
       ret.Add(propName, null); 
      } 
     } 

     return ret; 
    } 
0

我想添加反射的替代方法,使用JToken。您将需要检查两者之间的基准差异,以查看哪个具有更好的性能。

var location = new Location() { City = "London" }; 
var locationToken = JToken.FromObject(location); 
var locationObject = locationObject.Value<JObject>(); 
var locationPropertyList = locationObject.Properties() 
    .Select(x => new KeyValuePair<string, string>(x.Name, x.Value.ToString())); 

请注意,此方法最适合平面类结构。

+0

'var locationObject = locationObject.Value ();'? – 2018-02-28 04:01:15

相关问题