【发布时间】:2010-11-22 10:56:14
【问题描述】:
我的表达不是很好,我想改进它们,所以我想知道是否有人可以为我解释是否可以在类中创建一个可以在实例化期间赋予值的属性,如下所示:
new Column<Product>{ ColumnProperty = p => p.Title};
或者更好(但我认为我正在推动它)
new Column {ColumnProperty = p => p.Title};
类是这样的:
public class Column<TModel>
{
public Expression<Func<TModel, TProperty>> ColumnProperty { get; set; }
}
它背后的基本思想是我从一堆类似这样的 Column 对象创建一个 Grid。
List<Product> productList = GetProductsFromDb();
List<Column> columnList = new List<Column>();
columnList.Add(new Column<Product> {ColumnProperty = p => p.ProductId, Heading = "Product Id"});
columnList.Add(new Column<Product> {ColumnProperty = p => p.Title, Heading = "Title"});
Grid myGrid = new Grid(productList, columnList);
这可能不是最简洁/最简单的方法,但我很想知道是否可以这样做,因为我想提高对表达式的理解,并且我喜欢能够使用强类型值而不是字符串文字使用起来好多了。
任何想法、想法、漫谈将不胜感激
干杯 抢
【问题讨论】:
标签: c# lambda properties expression