简体   繁体   English

获取功能名称 <TSource, TKey> 通用类型参数

[英]Getting the Name of a Func<TSource, TKey> Generic Type Parameter

I'm using a Func as an argument for a method to be used in a GroupBy operation in a Linq statement. 我正在使用Func作为要在Linq语句中的GroupBy操作中使用的方法的参数。 TSource being IFoo and TKey being a property of IFoo such as IFoo.Number, making my Func parameter looking like: TSource是IFoo,而TKey是IFoo的属性,例如IFoo.Number,这使我的Func参数看起来像:

foo => foo.Number

So far so good. 到现在为止还挺好。 However, now I'm trying to get a string representation of TKey or rather, I'm trying the coax the string "Number" from the Func<> parameter. 但是,现在我正在尝试获取TKey的字符串表示形式,或更确切地说,我正在尝试从Func <>参数哄骗字符串“ Number”。

Can it be done and if so, how? 能做到吗?如果可以,怎么做? Thanking you all in advance.. 谢谢大家..

You can't with a Func, you need a parameter of type Expression<Func<TSource, TKey>> 您不能使用Func,需要一个Expression<Func<TSource, TKey>>类型的参数

and work on Expression's Body. 并致力于Expression的身体。

When you need the func, just compile the expression. 需要函子时,只需编译表达式。

To get name from expression : Retrieving Property name from lambda expression 从表达式获取名称: 从lambda表达式检索属性名称

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

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