Here are my classes with some required properties,
public class Employee
{
public EmployeeType employeeType {get; set;}
public Description description {get; set;}
}
public class EmployeeType
{
public NameType nameField {get; set;}
public string Address {get; set;}
}
public class NameType
{
[Required]
public string FirstName {get; set;}
[Required]
public string LastName {get; set;}
public Role RoleType {get; set;}
}
public class Role
{
public int RoleId {get; set;}
[Required]
public string Name {get; set;}
}
public class Description
{
[Required]
public string ShortDesciption {get; set;}
public string LongDescription {get; set;}
}
I have written a method to do a recursive check of properties but it does not go by Required annotation and it would not keep moving forward after finding first property with null value.
Here is my code,
private List<string> GetNullOrEmptyPropertiesList(object myObject)
{
List<string> lst = new List<string>();
foreach (PropertyInfo pi in myObject.GetType().GetProperties())
{
if (pi.PropertyType == typeof(string))
{
string value = (string)pi.GetValue(myObject);
if (string.IsNullOrEmpty(value))
{
if (pi.Name == "ShortDescription" || pi.Name == "FirstName")
{
lst.Add(pi.Name);
}
}
}
else
{
var value = pi.GetValue(myObject);
return GetNullOrEmptyPropertiesList(value);
}
}
return lst;
}
Aucun commentaire:
Enregistrer un commentaire