I have some code that enumerates over an object and records any errors it has based on its ValidationAttribute(s).
As it finds them I wish to create a collection of a custom class named RuleViolations. The RuleViolation class looks like this:
public string Message { get; set; }
public LambdaExpression Property { get; set; }
Property is a lambda expression so that the property doesn’t have to be a string. THis works when I manually add errors but I’m not sure how to specify the LambdaExpression when all I have is the property’s PropertyDescriptor object.
Does anyone know how?
LambdaExpression and PropertyDescriptor site largely in different worlds (much to my initial frustration). LambdaExpression is going to be interested in PropertyInfo, not PropertyDescriptor.
If you have the PropertyInfo, you can construct an Expression via:
You can also attempt to resolve by name, but this is not necessarily the same, especially if you are using a custom type model (
ICustomTypeDescriptoretc):