I have a custom validation class
using System;
using System.Collections.Generic;
using System.Reflection;
internal class RequiredAttribute1 : Attribute
{
public RequiredAttribute1()
{
}
public void Validate(object o, MemberInfo info, List<string> errors)
{
object value;
switch (info.MemberType)
{
case MemberTypes.Field:
value = ((FieldInfo)info).GetValue(o);
break;
case MemberTypes.Property:
value = ((PropertyInfo)info).GetValue(o, null);
break;
default:
throw new NotImplementedException();
}
if (value is string && string.IsNullOrEmpty(value as string))
{
string error = string.Format("{0} is required", info.Name);
errors.Add(error);
}
}
}
I am using it on the following object:-
class Fruit
{
[RequiredAttribute1]
public string Name {get; set;}
[RequiredAttribute1]
public string Description {get; set;}
}
Now, I want to run the validation rules on a list of fruits, to print to a string
All I can think of is :-
- Iterate through fruits
- For each fruit, iterate through its properties
- For each property, iterate through custom validators (only 1 here...)
- Call the Validate function
- Collect and print validator errors
Is there something easier than this and more built-in, for reading these annotations, without having to add framework dlls like (ASP.net / MVC /etc...) ?
This is just for a simple Console application.
Aucun commentaire:
Enregistrer un commentaire