表达式来; Func键< TModel的,TProperty>>作为对象初始化的属性?

编程入门 行业动态 更新时间:2024-10-25 16:28:53
本文介绍了表达式来; Func键< TModel的,TProperty>>作为对象初始化的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我的表达不是很好,我想改进他们,所以我想知道如果有人可以为我解释,如果可能在类中创建一个属性,可以在这样的实例化过程中赋予一个值:

新列< Product> {ColumnProperty = p => p.Title};

或更好的(但我认为我推着)

new Column {ColumnProperty = p => p.Title};

与这样的类:

public class Column< TModel> { public Expression< Func< TModel,TProperty>> ColumnProperty {get;组; } }

背后的基本思想是,我从一堆列对象是这样的。

列表<产品> productList = GetProductsFromDb(); 列表<列> 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);

这可能不是最简单/最简单的方法,但我有兴趣知道是否可以要做到这一点,我想提高我对表达的理解,我喜欢能够使用强类型值而不是字符串字面量,这比使用更好。

任何想法,想法,漫游将不胜感激

Cheers Rob

解决方案

当您定义类似 Func< TModel,TProperty> 的内容时,表示有一种方法可以使用TModel类型并返回TProperty类型。所以lambda表达式如

p => p.ProductId

返回属性的值而不是属性本身。

您可以做什么来使此语法工作是如下定义您的 ColumnProperty :

public Expression< Func< TModel,object>> ColumnProperty {get;组;

然后,您可以从表达式树中提取详细信息。

My expression aren't great and I would like to improve on them so I am wondering if someone could explain for me if it possible to create a property in a class that can be given a value during instantiation like this:

new Column<Product>{ ColumnProperty = p => p.Title};

or better still (but I think I am pushing it)

new Column {ColumnProperty = p => p.Title};

with a class something like this:

public class Column<TModel> { public Expression<Func<TModel, TProperty>> ColumnProperty { get; set; } }

The basic idea behind it is that I create a Grid from a bunch of Column objects something 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);

This may not be the tidiest/easiest way to do this but I am interested in knowing whether it can be done as I want to improve my understanding of expressions and I love being able to have strongly typed values instead of string literals it's so much nicer to work with.

Any thoughts, ideas, ramblings would be greatly appreciated

Cheers Rob

解决方案

When you define something like Func<TModel, TProperty> it means there is a method that takes a TModel type and returns a TProperty type. So lambda expressions like

p => p.ProductId

returns the value of the property rather than the property itself.

What you can do to make this syntax work is to define your ColumnProperty as follows:

public Expression<Func<TModel, object>> ColumnProperty { get; set; }

Then you can extract detailed information from the expression tree.

更多推荐

表达式来; Func键&LT; TModel的,TProperty&GT;&GT;作为对象初始化的属性?

本文发布于:2023-11-10 00:38:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573884.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   初始化   属性   对象   amp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!