c# - Validate a list in a custom validation attribute -


using custom validation attribute, i'm trying validate list of objects. class shown below: myclass. in custom attribute, i'm able covert object valueinto list of objects, how can access properties, id , name within objects?

i understand put attribute directly on the myclass properties, need check couple things before validation. first, need check if number of items in list greater one. then, if count greater one, need check if both id , name null.

my thought easier validate entire list in 1 attribute. if did place attributes on myclass properties, still need access values within list.

if list passed value parameter of isvalid list items within value. if put attributes on myclass properties need values way. i've been able single values code below, i'm unable list of values.

bottom line: need way access values within list either object value parameter or validationcontext validationcontext parameter.

getting single value (not list of values):

var property = validationcontext.objecttype.getproperty("myproperty"); var value = property.getvalue(validationcontext.objectinstance, null); 

custom attribute:

public sealed class validatenewsaulist : validationattribute {     protected override validationresult isvalid(object value, validationcontext validationcontext)     {         var mylist = value ilist;     }  } 

here user defined class:

public class myclass {     public int id { get; set; }     public string name { get; set; } } 

the business logic bit unclear, so, based on qualified guess, solution following. assuming have _lst = list<myclass>, can iterate through list , apply validation rules (e.g. referred methods validateid() , validatename()) both properties:

foreach (myclass _mc in _lst) {    validateid (_mc.id);    validatename (_mc.name);  } 

hope may help. best regards,


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -