Is there any way to ensure that I can only pass in an expression that points to property on the a class?
public class Foo
{
public string Bar { get; set; }
public void DoSomething()
{
HelperClass.HelperFunc(() => Bar);
}
}
public static class HelperClass
{
public static void HelperFunc(Expression<Func<string>> expression)
{
// Ensure that the expression points to a property
// that is a member of the class Foo (or throw exception)
}
}
Also, if need be, I can change the signature to pass the actual class in as well…
Here is extension method, which converts lambda to property. If lambda does not points to property, exception is thrown