There are a lot of benefits for using lambda expression to capture property or method of some class like the following code.
void CaptureProperty<T, TProperty> (Func<T, TProperty> exp)
{
// some logic to keep exp variable
}
// So you can use below code to call above method.
CaptureProperty<string, int>(x => x.Length);
However, the above code does not support static property. So, how to create method that support both static property and non-static property?
Thanks,
Well, you can capture a static property that way:
You then need to provide a “dummy” value at execution time though…
An alternative would be to provide another overload with only a single type argument:
Use is like this:
Is that what you’re after?
If you wanted to unify the two internally, you could have a “dummy” private nested type within the same type as
CapturePropertyand implement the static version like this:Then you could detect that the “source” type is
DummyTypewhen you need to call the function later. This may or may not be a useful idea depending on what else you’re doing 🙂