The expression <Func <TModel, TProperty >> as a property to initialize an object?
My expression is not very large, and I would like to improve them, so I wonder if someone can explain to me whether it is possible to create a property in a class that can be assigned a value during instance creation, for example:
new Column<Product>{ ColumnProperty = p => p.Title};
or even better (but I think I click it)
new Column {ColumnProperty = p => p.Title};
with the class something like this:
public class Column<TModel>
{
public Expression<Func<TModel, TProperty>> ColumnProperty { get; set; }
}
The basic idea is that I create a Grid from a group of Column objects like this.
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);
It may not be the easiest / easiest way to do this, but I am interested to know if it can be done, because I want to improve my understanding of expressions, and I like to have strongly typed values instead of string literals, so it works better.
Any thoughts, ideas, firewalls will be greatly appreciated.
Cheers Rob
+5
2