Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 迭代法_C#_Visual Studio_Oop - Fatal编程技术网

C# 迭代法

C# 迭代法,c#,visual-studio,oop,C#,Visual Studio,Oop,我有一个IList,它保存一个对象,该对象包含字段name、Id、location、store、person和amount。我不想通过为每个属性编写语句来检索所有这些字段的值 前 我是否可以遍历此列表并检索其字段中的数据,而不必特别告诉我它们是什么?任何协助都将不胜感激 使用 list而不是IList如果要检索项的所有属性的值,可以使用反射创建检索属性值的函数列表: List<Person> people = new List<Person>(); people.Add(

我有一个IList,它保存一个对象,该对象包含字段name、Id、location、store、person和amount。我不想通过为每个属性编写语句来检索所有这些字段的值

我是否可以遍历此列表并检索其字段中的数据,而不必特别告诉我它们是什么?任何协助都将不胜感激

使用
list而不是IList

如果要检索项的所有属性的值,可以使用反射创建检索属性值的函数列表:

List<Person> people = new List<Person>();
people.Add(new Person() {Id = 3, Location = "XYZ"});

var properties = (from prop in typeof (Person).GetProperties(BindingFlags.Public | BindingFlags.Instance)
                    let parameter = Expression.Parameter(typeof (Person), "obj")
                    let property = Expression.Property(parameter, prop) 
                    let lambda = Expression.Lambda<Func<Person, object>>(Expression.Convert(property, typeof(object)), parameter).Compile()
                    select
                    new
                        {
                            Getter = lambda,
                            Name = prop.Name
                        }).ToArray();


foreach (var person in people)
{
    foreach (var property in properties)
    {
        string name = property.Name;
        object value = property.Getter(person);
        //do something with property name / property value combination.

    }
}

也可以使用反射来检索属性值,但这相当慢,如果您有一个很长的列表/许多属性,则可能会引起注意。

您想对数据做什么?试试这个您想做什么?在很多情况下,序列化、数据绑定都不需要指定名称,如果使用适当的机制,我只是尝试将来自这些字段的值放入字符串变量中。对不起,IList比刚性列表更具弹性。现在,我只需要牢牢掌握它的功能。
List<Person> people = new List<Person>();
people.Add(new Person() {Id = 3, Location = "XYZ"});

var properties = (from prop in typeof (Person).GetProperties(BindingFlags.Public | BindingFlags.Instance)
                    let parameter = Expression.Parameter(typeof (Person), "obj")
                    let property = Expression.Property(parameter, prop) 
                    let lambda = Expression.Lambda<Func<Person, object>>(Expression.Convert(property, typeof(object)), parameter).Compile()
                    select
                    new
                        {
                            Getter = lambda,
                            Name = prop.Name
                        }).ToArray();


foreach (var person in people)
{
    foreach (var property in properties)
    {
        string name = property.Name;
        object value = property.Getter(person);
        //do something with property name / property value combination.

    }
}