Skip to main content
Post Made Community Wiki by Jamal
Source Link

Seems like ive found a great solution, my entire loop can be rewritten as this:

InvokeOnAttr<DisplayAttribute>(property, a => {
    Name = a.Name;
    Group = a.GroupName;
    Description = a.Description;
});

InvokeOnAttr<MinLengthAttribute>(property, a => {
    Validate.MinLength = a.Length;
});

InvokeOnAttr<MaxLengthAttribute>(property, a => {
    Validate.MaxLength = a.Length;
});

InvokeOnAttr<RegularExpressionAttribute>(property, a => {
    Validate.Regex = a.Pattern;
    Validate.RegexError = a.ErrorMessage;
});

If this method also is availible in the class (it takes a property, finds the attribute (of type TAttr provided), and if found executes the passed in action on it:

public void InvokeOnAttr<TAttr>(PropertyInfo p, Action<TAttr> action) where TAttr : Attribute {
    TAttr attr = p.GetCustomAttribute<TAttr>();
    if (attr is ValidationAttribute)
    {
        if (Validate == null) Validate = new MetaValidate();
    }
    if (attr != null) { 
        action.Invoke(attr);
    }
}