mardi 2 mars 2021

How to validate properties via reflection after each user input on console application

I am requesting user input based on the properties I am grabbing which are all strings. Where User contains name, lastname, email, etc.

I wanted to input value to Person and validate per line. So if the user enters first property incorrectly, a validation error will print out.

Person p = new Person();

foreach(PropertyInfo pi in p.GetType().GetProperties())
{
    Console.WriteLine($"Enter {pi.Name}");
    string input = Console.ReadLine();
                
}

Below, I can check one by one, just figuring out how I can incorporate this to add for above.

RegistrationValidation rv = new RegistrationValidation();

p.FirstName = "";
p.Email = "";
p.LastName = "";
            
ValidationResult results = rv.Validate(p);

if (results.IsValid == false)
{
   foreach (ValidationFailure failure in results.Errors)
    {
       System.Console.WriteLine($"{failure.PropertyName}: {failure.ErrorMessage}");
    }
}




Aucun commentaire:

Enregistrer un commentaire