C# 如何从c语言中的字典值中提取数据#

C# 如何从c语言中的字典值中提取数据#,c#,dictionary,C#,Dictionary,我有一个代码,可以从中获取字典值中的ViewModel。如何提取各个值 foreach (KeyValuePair<string, object> kvp in dictionary) { var key = kvp.Key; var value = kvp.Value; //foreach (var vp in value) //{

我有一个代码,可以从中获取字典值中的ViewModel。如何提取各个值

        foreach (KeyValuePair<string, object> kvp in dictionary)
        {
            var key = kvp.Key;
            var value = kvp.Value;
            //foreach (var vp in value)
            //{

            //}

        }
foreach(字典中的KeyValuePair kvp)
{
var key=kvp.key;
var值=kvp.值;
//foreach(价值中的var vp)
//{
//}
}
需要从KVP.values中提取值

您可以使用:

PropertyInfo[]myPropertyInfo;
//获取“Type”类对象的属性。
myPropertyInfo=value.GetType().GetProperties();
WriteLine(“System.Type的属性为:”);
for(int i=0;i
我要提到的是,提出此问题的人试图从属性中获取值,以便通过以下方式实现:

using System;
using System.Collections.Generic;
using System.Linq;

namespace StoVerF1
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, Car> dictionarySet = new Dictionary<string, Car>();
            dictionarySet.Add("Audi", new Car { maxSpeed = 220 });
            dictionarySet.Add("BMW", new Car { maxSpeed = 235 });
            dictionarySet.Add("Ferrari", new Car { maxSpeed = 285 });

            var dataFromReflectionKeys = (dictionarySet.Keys as IEnumerable<string>)?.ToList();
            foreach (string keyDic in dataFromReflectionKeys)
            {
                Console.WriteLine($"For auto {keyDic} type info :");

                var dictElem = (dictionarySet[keyDic] as Car);
                if (dictElem != null)
                {
                    var propeties = dictElem.GetType().GetProperties();
                    foreach (var prop in propeties)
                    {
                        Console.WriteLine($"Value of property{prop.Name} is {prop.GetValue(dictElem)}");
                    }
                    var fields = dictElem.GetType().GetFields();
                    var methods = dictElem.GetType().GetMethods();
                };  
            }
            
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
名称空间StoVerF1
{
班级计划
{
静态void Main(字符串[]参数)
{
Dictionary dictionarySet=新字典();
添加(“奥迪”,新车{maxSpeed=220});
添加(“宝马”,新车{maxSpeed=235});
dictionarySet.Add(“法拉利”,新车{maxSpeed=285});
var dataFromReflectionKeys=(dictionarySet.Keys作为IEnumerable)?.ToList();
foreach(ReflectionKeys数据中的字符串keyDic)
{
WriteLine($”表示自动{keyDic}类型信息:);
var dictElem=(字典设置[keyDic]为Car);
if(dictElem!=null)
{
var properties=dictElem.GetType().GetProperties();
foreach(房地产中的var prop)
{
WriteLine($“属性{prop.Name}的值为{prop.GetValue(dictElem)}”);
}
var fields=dictElem.GetType().GetFields();
var methods=dictElem.GetType().GetMethods();
};  
}
}
}
}

在字典的值部分有一个
对象
类型。您可以将
对象
向下转换到
特定类型
,以便能够直接访问对象属性。因此:

using System.Linq;

var query = from obj in dictionary.Values
            select obj as SpecificType;

foreach (var item in query)
{
}

键值对(当前对象)的值是否总是属于同一类型?那么“var value”是否总是包含相同的属性?对于字段也可以这样做
using System.Linq;

var query = from obj in dictionary.Values
            select obj as SpecificType;

foreach (var item in query)
{
}