Paul Siersma
Paul Siersma

Reputation: 2206

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

Upvotes: 0

Views: 666

Answers (1)

Rapha&#235;l Althaus
Rapha&#235;l Althaus

Reputation: 60503

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

Upvotes: 4

Related Questions