作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个场景,我需要获取类属性的值的集合。
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;
}
}
我想避免太多的迭代。任何想法都会有所帮助。
最佳答案
一些 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);
关于c# - 如何从类的集合中获取类的属性列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16959156/
我是一名优秀的程序员,十分优秀!