jeudi 1 septembre 2016

How to add DataAnnotations attributes to specific properties in a C# class

I wanted to add different DataAnnotations attributes to different instances of My Answer class which is as follows:

 public class AnswerViewModel()
   {
   public String QuestionText {get;set;}
   public String AnswerText {get;set;}
   }

I want to assign different attributes to the QuestionText field in different instances of this class. Like in

AnswerViewModel a1 = new AnswerViewModel();
AnswerViewModel a2 = new AnswerViewModel();

I want the QuestionText field in a1 to have the DateTimeAttribute applied and in a2 the QuestionText field should have the PasswordAttribute applied and so on. I know this will be done via Reflection or the TypeDescriptor but I am completely lost as to how it will be done.





Aucun commentaire:

Enregistrer un commentaire