简体   繁体   English

如何拆分在C#中创建匿名类型的表达式

[英]How to split an Expression that creates anonymous types in C#

I have a expression in C# 我在C#中有一个表达式

abc => new { abc.x, abc.y }

i want to break it down to 我想将其分解为

abc => abc.x
abc => abc.y
...

so i can loop through each argument. 这样我就可以遍历每个参数。 Anyone shed some light on this? 有人对此有所启示吗?

Anonymous declarations are just an abstraction for an anonymous type generated at compile time . 匿名声明只是编译时生成的匿名类型的抽象。

You won't be able to generate them on the fly like you describe. 您将无法像您描述的那样即时生成它们。

I have no idea why do you want to do this, but mellamokb comment shows the direction. 我不知道您为什么要这样做,但是mellamokb的注释显示了方向。 The whole method could look something like this: 整个方法如下所示:

IEnumerable<Expression> GetExpressions<TParam, TResult>(
    Expression<Func<TParam, TResult>> expression)
{
    return ((NewExpression)expression.Body).Arguments
        .Select(a => Expression.Lambda(a, expression.Parameters.Single()));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM