C# 如何从类的集合中获取类的属性列表
我有一个场景,需要获取类的属性的值集合C# 如何从类的集合中获取类的属性列表,c#,linq,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(s
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;
}
}
公共类人物
{
公共字符串名称{get;set;}
公共字符串属性1{get;set;}
公共字符串属性2{get;set;}
}
公共类PersonCollection:列表
{
公共对象[]获取值(字符串propertyName)
{
//实现此方法的最佳方法是什么?
返回null;
}
}
我想避免太多的迭代。任何想法都会有帮助。如果您正在使用实体框架,
您可以使用
this.GetAll()代码>一个简单的解决方案是使用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;
}
}
使用反射
人员p=新人员();
object obj=p.GetType().GetProperty(propertyName).GetValue(p,null) 试试这个
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();
}
public对象[]获取值(字符串propertyName)
{
列表结果=新列表();
PropertyInfo PropertyInfo=typeof(Person).GetProperty(propertyName);
this.ForEach(person=>result.Add(propertyInfo.GetValue(person));
返回result.ToArray();
}
这是一个工作程序
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();
}
}
公共类人物
{
公共字符串名称{get;set;}
公共字符串属性1{get;set;}
公共字符串属性2{get;set;}
}
公共类PersonCollection:列表
{
公共对象[]获取值(字符串propertyName)
{
var result=新列表();
foreach(本表中的个人项目)
{
添加(item.GetType().GetProperty(propertyName).GetValue(item));
}
返回result.ToArray();
}
}
班级计划
{
静态void Main(字符串[]参数)
{
var collection=new PersonCollection();
添加(newperson(){Name=“George”,Property1=“aaa”,Property2=“bbbb”});
添加(newperson(){Name=“Peter”,Property1=“ccc”,Property2=“dddd”});
var objects=collection.GetValues(“Property1”);
foreach(对象中的对象项)
{
Console.WriteLine(item.ToString());
}
Console.Read();
}
}
一点Linq魔力:
public object[] GetValues(Expression<Func<Person, object>> exp)
{
var function = exp.Compile();
return this.Select(function).ToArray();
}
不使用反射的简单想法如下:
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();
}
}
不幸的是,我没有使用它。
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);
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();
}
}
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;
}
}