1

I am using FileHelper library. Actually I want to record the errors after reading a file. so while recording the errors I want name of property of mapped class. So how can I do it.

let say my class is -

public class Employee
{
  public string EmployeeName;
  public string EmployeeCity
}

for this class let say employeename is mandatory so i will validate my record after file read so now i want to log a message that "EmployeeName is mandatory" so i want EmployeeName should come dynamically. can i do it ?

Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191
Kumar
  • 7
  • 2

1 Answers1

0

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);
    }
}
Den
  • 360
  • 1
  • 9
  • Actually FileHelper is a third party library for parsing the files. it loads the data written in file into array of your own type. so in my example after reading a file i will be having data loaded in Employee[] array. so now i will loop through each object and validate for mandatory case. during this validation i want to log the field name for which validation has got failed. so i will be doing manual validation only like String.IsNullOrMandatory(emp.EmployeeName) so if this property value is null or empty then i want to log message "Employee Name is mandatory" – Kumar Jul 24 '14 at 02:40
  • @user3868217, my scenario fits your reqs, check the edit in answer – Den Jul 24 '14 at 03:45
  • OK...other way might be create a custom attribute whiich will tell name of field. i understand, you are trying to solve the problem by reflection. is there any other way. reflection is bit slow. – Kumar Jul 24 '14 at 05:07
  • i do not want to use the reflection just to read the field name. actually field name may be different than property name. i mean i want to set "Employee Name" instead of "EmployeeName" see the space diff. – Kumar Jul 24 '14 at 05:09
  • @user3868217, Well, the only way I know is DataAnnotation - decorating props with Display attribute, then get it with fore example with an extesnion methode like here [How to retrieve Data Annotations from code](http://stackoverflow.com/questions/7027613/how-to-retrieve-data-annotations-from-code-programmatically) But don't have ideas how to do it without reflections... a static dictionary of required field names maybe. Maybe someone else can help you. Good luck – Den Jul 24 '14 at 05:58
  • and here a link for manual validation of data annotations, maybe that can help [Manual validation with data annotations](http://www.campusmvp.net/blog/manual-validation-with-data-annotations) – Den Jul 24 '14 at 06:08
  • Ok...i will wait for others to comment or provide some other solution which is not based on reflection. but one good link you gave me which talks about validation through dataannotation...but that also does not resolve my problem but good idea to do validation through attributes – Kumar Jul 24 '14 at 07:16