You need reflections I think. You can use YourClassInstance.GetType().GetProperties()
to get a list of properties of an object. public string EmployeeName;
- it’s a field. public string EmployeeName {get;set;}
– it’s a prop. But you mentioned mandatory. How do you recognize mandatory? I can suppose that you mark them with a kind of a custom attribute:
[AttributeUsage(AttributeTargets.Property)]
public class MandatoryAttribute:System.Attribute
{
}
public class Employee
{
[Mandatory]
public string EmployeeName{get;set;}
[Mandatory]
public string EmployeeCity{get;set;}
public string EmployeeAccount{get;set;}
}
Now to get a list of names of mandatory props of object you can do following:
Employee c=new Employee();
var propNames= c.GetType().GetProperties().Where(pinf=>pinf.GetCustomAttributes(true).FirstOrDefault(a => a.GetType()==typeof(MandatoryAttribute))!=null).ToList().Select(d=>d.Name);
This will bring you a list containing {"EmployeeName","EmployeeCity"}
EDIT
var EmployeeCollection=Helper.GetEmployees();
foreach(var employee in EmployeeCollection)
{
foreach(var prop in employee.GetType().GetProperties())
{
string val=(string)prop.GetValue(employee,null);
if(String.IsNullOrEmpty(val)) Helper.Log(prop.Name);
}
}