如何从其集合中获取类的属性列表

时间:2013-06-06 10:00:47

标签: c# linq

我有一个场景,我需要获取类的属性值的集合。

public class Person
{
    public string Name { get; set; }
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

public class PersonCollection : List<Person>
{
    public object[] GetValues(string propertyName)
    {
        // best way to implement this method?
        return null;
    }
}

我想避免多次迭代。任何想法都会有所帮助。

7 个答案:

答案 0 :(得分:4)

一点Linq魔术:

public object[] GetValues(Expression<Func<Person, object>> exp)
{
    var function = exp.Compile();
    return this.Select(function).ToArray();
}

用法:

// assuming coll in a PersonCollection
var names = coll.GetValues(p => p.Name);

答案 1 :(得分:2)

一个简单的解决方案是使用LINQ的Select方法:

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

public class Person
{
    public string Name { get; set; }
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

public class PersonCollection : List<Person>
{
    public object[] GetValues(string propertyName)
    {
        if (propertyName == "Name")
        {
            return this.Select(p => p.Name).ToArray();
        }
        else if (propertyName == "Property1")
        {
            return this.Select(p => p.Property1).ToArray();
        }
        else if (propertyName == "Property2")
        {
            return this.Select(p => p.Property1).ToArray();
        }

        // best way to implement this method?
        return null;
    }
}

您还可以使用表达式树来允许将类型安全的访问者lambda用作参数:

public object[] GetValues(Expression<Func<Person, object>> propertyNameExpression)
{
    var compiledPropertyNameExpression = propertyNameExpression.Compile();

    if (propertyNameExpression.Body.NodeType == ExpressionType.MemberAccess)
    {
        return this.Select(compiledPropertyNameExpression).ToArray();
    }

    throw new InvalidOperationException("Invalid lambda specified. The lambda should select a property.");
}

然后您可以按如下方式使用它:

var personNames = personCollection.GetValues(p => p.Name)

答案 2 :(得分:2)

不使用反射的简单想法就是这样:

public partial class PersonCollection: List<Person> {
    public object[] GetValues(String propertyName) {
        return (
            from it in this
            let x=
                "Name"==propertyName
                    ?it.Name
                    :"Property1"==propertyName
                        ?it.Property1
                        :"Property2"==propertyName
                            ?it.Property2
                            :default(object)
            where null!=x
            select x).ToArray();
    }
}

但是我宁愿返回IEnumerable而不是急切地列举:

public partial class PersonCollection: List<Person> {
    public IEnumerable GetValues(String propertyName) {
        return
            from it in this
            let x=
                "Name"==propertyName
                    ?it.Name
                    :"Property1"==propertyName
                        ?it.Property1
                        :"Property2"==propertyName
                            ?it.Property2
                            :default(object)
            where null!=x
            select x;
    }
}

答案 3 :(得分:1)

使用反射

人P =新人(); object obj = p.GetType()。GetProperty(propertyName).GetValue(p,null);

答案 4 :(得分:1)

试试这个,

public object[] GetValues(string propertyName)
{
    List<object> result = new List<object>();
    PropertyInfo propertyInfo = typeof(Person).GetProperty(propertyName);
    this.ForEach(person => result.Add(propertyInfo.GetValue(person)));
    return result.ToArray();
}

答案 5 :(得分:1)

这是一个工作程序

public class Person
{
    public string Name { get; set; }
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

public class PersonCollection : List<Person>
{
    public object[] GetValues(string propertyName)
    {
        var result = new List<object>();
        foreach (Person item in this)
        {
            result.Add(item.GetType().GetProperty(propertyName).GetValue(item));
        }
        return result.ToArray();
    }
}
class Program
{
    static void Main(string[] args)
    {
        var collection = new PersonCollection();
        collection.Add(new Person(){Name = "George", Property1 = "aaa", Property2 = "bbbb"});
        collection.Add(new Person(){Name = "Peter", Property1 = "ccc", Property2 = "dddd"});
        var objects = collection.GetValues("Property1");
        foreach (object item in objects)
        {
            Console.WriteLine(item.ToString());
        }
        Console.Read();
    }
}

答案 6 :(得分:0)

如果您正在使用实体框架, 你可以使用this.GetAll();