<p>I'm looking to create an ValidationRule class that validates properties on an entity type object.  I'd really like to set the name of the property to inspect, and then give the class a delegate or a lambda expression that will be evaluated at runtime when the object runs its IsValid() method.  Does anyone have a snippet of something like this, or any ideas on how to pass an anonymous method as an argument or property?</p>

<p>Also, I'm not sure if I'm explaining what I'm trying to accomplish so please ask questions if I'm not being clear.</p>

```
```
class ValidationRule {
    public delegate bool Validator();

    private Validator _v;

    public ValidationRule(Validator v) { _v = v; }

    public Validator Validator {
        get { return _v; }
        set { _v = value; }
    }

    public bool IsValid { get { return _v(); } }
}

var alwaysPasses = new ValidationRule(() => true);
var alwaysFails = new ValidationRule(() => false);

var textBoxHasText = new ValidationRule(() => textBox1.Text.Length > 0);
```
```
That should get you started. But, really, inheritance is far more appropriate here. The problem is simply that the
```
Validator
```
doesn't have access to any state that it doesn't close over, this means that it isn't as reusable as say
```
ValidationRules
```
that contain their own state. Compare the following class to the previous definition of
```
textBoxHasText
```
.
```
```
interface IValidationRule {
    bool IsValid { get; }
}

class BoxHasText : IValidationRule {
    TextBox _c;

    public BoxHasText(TextBox c) { _c = c; }

    public bool IsValid {
        get { return _c.Text.Length > 0; }
    }
}
```
```