0

How can I get the property name from Func<T, TResult>?

There many posts how to get prop name but from Expression and not from Func

_resultViewModel.SelectMeasurement(si => si.Height, vm => vm.HeightImg); // this is usage... I need to get "Height"

public void SelectMeasurement(Func<ScanInfo, double> measurement, Func<ResultViewModel, ImageSource> image)
{
    //some stuff
}
Wai Ha Lee
  • 8,173
  • 68
  • 59
  • 86
demo
  • 5,676
  • 16
  • 65
  • 141

1 Answers1

1

You can't get "property name" from Func<T, TResult>, because there are no any "property" and any "name", when delegate is constructed.

Moreover, delegate can get its return value in some different way, instead of member access:

Func<Foo, string> = foo => "bar";

This differs from the expressions case (Expression<Func<T, TResult>>), because expressions represent some code, which could be compiled into delegate, and could be parsed.

Dennis
  • 35,951
  • 9
  • 78
  • 143
  • ok, thanks for reply. If i use ` Expression>`, how can i get `Func` from expression? – demo Nov 02 '15 at 12:20
  • 1
    @demo: you can compile it: `yourExpressionVar.Compile()`. Note, that it rather slow, and it is better to think about caching of results. – Dennis Nov 02 '15 at 12:27