当前位置:网站首页>Expression "func" tSource, object "to expression" func "tSource, object" []
Expression "func" tSource, object "to expression" func "tSource, object" []
2022-04-23 16:53:00 【Tassel 1990】
// Handle multiple attribute generation Expression expression , from Expression<Func<TSource, object>> To Expression<Func<TSource, object>>[], Single to multiple
// from x=>new{x.a,x.b} To x=>x.a x=>x.b
private static Expression<Func<TSource, object>>[] ProcessExpressionObjet<TSource>(Expression<Func<TSource, object>> propertyExpression)
{
Expression<Func<TSource, object>>[] expressions = new Expression<Func<TSource, object>>[] { };
Expression expression = propertyExpression.Body;
while (expression.NodeType == ExpressionType.Convert || expression.NodeType == ExpressionType.ConvertChecked)
expression = ((UnaryExpression)expression).Operand;
//Parameter
ParameterExpression parameterExpression = propertyExpression.Parameters.Single<ParameterExpression>();
if (expression is NewExpression)
{
NewExpression newExpression = expression as NewExpression;
if (newExpression == null)
return expressions;
expressions = new Expression<Func<TSource, object>>[newExpression.Arguments.Count];
int index = 0;
foreach (Expression subExpression in newExpression.Arguments)
{
MemberExpression member = subExpression as MemberExpression;
Expression exProperty = Expression.Property(parameterExpression, member.Member.Name);
var body = Expression.Convert(exProperty, typeof(object));
expressions[index] = Expression.Lambda<Func<TSource, object>>(body, parameterExpression);
index++;
}
return expressions;
}
else if (expression is MemberExpression)
{
expressions = new Expression<Func<TSource, object>>[1];
Expression exProperty = Expression.Property(parameterExpression, (expression as MemberExpression).Member.Name);
var body = Expression.Convert(exProperty, typeof(object));
expressions[0] = Expression.Lambda<Func<TSource, object>>(body, parameterExpression);
return expressions;
}
else
return expressions;
}
Source idea :
Get entities T Of all properties of lambda Expression array : Such as x->x.a,x->x.b,x->x.b,x->x.c
public static Expression<Func<T, object>>[] GetExpressions<T>()
{
var properties = typeof(T).GetProperties();
Expression<Func<T, object>>[] expressions = new Expression<Func<T, object>>[properties.Length];
var p = Expression.Parameter(typeof(T), "x");
for (int i = 0; i < properties.Length; i++)
{
Expression exProperty = Expression.Property(p, properties[i]);
var body = Expression.Convert(exProperty, typeof(object));
expressions[i] = Expression.Lambda<Func<T, object>>(body, p);
}
return expressions;
}
版权声明
本文为[Tassel 1990]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231359253845.html
边栏推荐
- ACL 2022 | DialogVED:用于对话回复生成的预训练隐变量编码-解码模型
- BUG_ me
- 100 deep learning cases | day 41 - convolutional neural network (CNN): urbansound 8K audio classification (speech recognition)
- Rtklib 2.4.3 source code Notes
- Use if else to judge in sail software - use the title condition to judge
- Deepinv20 installation MariaDB
- ByteVCharts可视化图表库,你想要的我都有
- How to build tiktok user trust and drive fan growth
- Quick install mongodb
- Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
猜你喜欢
Nacos detailed explanation, something
True math problems in 1959 college entrance examination
Project framework of robot framework
Real time operation of vim editor
Feign report 400 processing
面试百分百问到的进程,你究竟了解多少
信息摘要、数字签名、数字证书、对称加密与非对称加密详解
LVM与磁盘配额
How magical is the unsafe class used by all major frameworks?
MySQL master-slave synchronization pit avoidance version tutorial
随机推荐
About background image gradient()!
5分钟NLP:Text-To-Text Transfer Transformer (T5)统一的文本到文本任务模型
∑GL-透视投影矩阵的推导
Copy constructor shallow copy and deep copy
Installing labellmg tutorial in Windows
如何用Redis实现分布式锁?
计组 | 【七 输入/输出系统】知识点与例题
Pseudo Distributed installation spark
Nodejs installation and environment configuration
Path environment variable
About stream flow, write it down briefly------
True math problems in 1959 college entrance examination
DanceNN:字节自研千亿级规模文件元数据存储系统概述
Use case execution of robot framework
vscode如何比较两个文件的异同
Public variables of robotframework
oracle 中快速获取表的列名列表
计算饼状图百分比
Cartoon: what are IAAs, PAAS, SaaS?
拷贝构造函数 浅拷贝与深拷贝