简体   繁体   中英

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. TSource being IFoo and TKey being a property of IFoo such as IFoo.Number, making my Func parameter looking like:

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.

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>>

and work on Expression's Body.

When you need the func, just compile the expression.

To get name from expression : Retrieving Property name from lambda expression

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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