c# - Trim actions for all properties -


i have typical website admin part admin can add many different entities. i, developer, have trim each of them (to prevent enter entities ' status name '. i.e. in validate method ivalidatableobject interface:

   public class addprojectviewmodel : projectformbaseviewmodel, ivalidatableobject     {         public int? parentprojectid { get; set; }          public ienumerable<validationresult> validate(validationcontext validationcontext)         {             projectname = projectname.trim();             descriptiontext = descriptiontext.trim(); 

of course, can in method project adding db or else. if have 10 forms , each form has 2-3 string properties, code little "straight". maybe can recommend other, more "beautiful" approach trim string parameters? i.e. via attribute of property or else?

why don't use reflection?

var obj = yourobjecttobetrimmed(); foreach(var property in obj.gettype().getproperties().where(x => x.propertytype == typeof(string))) {     property.setvalue(obj, (property.getvalue(obj) string).trim()); } 

also 1 can use attributes or other modifications of reflection.

edit. modify answer due op's request. in following code properties marked trimattribute trimmed.

class program {         static void main(string[] args) {             // sample properties.             var nottrimmedstring = "  smth   ";             var trimmedstring = nottrimmedstring.trim();              // prepare object trim properties.             var obj = new {                 propertytobetrimmed = nottrimmedstring,                 propertynottobetrimmed = nottrimmedstring,             };              // trim properties marked trimattribute.             foreach(var property in obj.gettype().getproperties().where(x =>                  x.propertytype == typeof(string) &&                 x.getcustomattributes(typeof(trimattribute), true).any())) {                 property.setvalue(obj, (property.getvalue(obj) string).trim());             }              // check.             console.writeline(obj.propertytobetrimmed == nottrimmedstring);             console.writeline(obj.propertynottobetrimmed == nottrimmedstring);             console.writeline(obj.propertytobetrimmed == trimmedstring);             console.writeline(obj.propertynottobetrimmed == trimmedstring);             console.readkey();         }     }      /// <summary>     /// sample class.     /// </summary>     class {         /// <summary>         /// property must marked trimattribute.          /// trimmed.         /// </summary>         [trim]         public string propertytobetrimmed { get; set; }          /// <summary>         /// property must not marked trimattribute.          /// not trimmed.         /// </summary>         public string propertynottobetrimmed { get; set; }     }      /// <summary>     /// custom attribute means need trimming.     /// </summary>     class trimattribute : attribute { } 

i suppose this tutorial if new reflection , attributes.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -