是否可以将IEnumerable<KeyValuePair<string,string>>KeyValuePair的aa转换为匿名类型?
Dictionary<string, string> dict= new Dictionary<string, string>();
dict.add("first", "hello");
dict.add("second", "world");
var anonType = new{dict.Keys[0] = dict[0], dict.Keys[1] = dict[1]};
Console.WriteLine(anonType.first);
Console.WriteLine(anonType.second);
********************output*****************
hello
world
Run Code Online (Sandbox Code Playgroud)
我想这样做的原因是因为我从web服务中检索一个对象,该对象表示wsdl中不存在的对象.返回的对象仅包含KeyValuePair集合,该集合包含自定义字段及其值.这些自定义字段可以命名为任何内容,因此我无法将xml反序列化方法映射到我将使用的最终对象(其属性必须绑定到网格).
*仅仅因为我使用Dictionary<string,string>并不意味着它绝对是一本字典,我只是用它来说明.真的是它IEnumerable<KeyValuePair<string,string>>
我一直试图想办法做到这一点,但我画了一个空白.这是c#.NET 4.0.