有什么办法可以确保我只能传入一个指向类属性的表达式?
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)
}
}
另外,如果需要,我可以更改签名以传递实际的课程......