Blog Home  Home Feed your aggregator (RSS 2.0)  
kevin Mocha - Attribute programming in .Net
Bookmarks collected from web.
 
 Monday, April 21, 2008

http://www.developerfusion.co.uk/show/2936/4/

   1:  using System;
   2:  using System.IO;
   3:  using System.Runtime.Serialization.Formatters.Soap;
   4:  using System.Reflection;
   5:  using System.Collections;
   6:   
   7:  [Serializable]
   8:  public class User{
   9:      [ValidLength(4,8,Message="UserID should be between 4 and 8 characters long")]
  10:      public string userID;
  11:   
  12:      [ValidLength(4,8,Message="Password should be between 4 and 6 characters long")]
  13:      public string password;
  14:   
  15:      [ValidLength(4,60)]
  16:      public string email;
  17:      public string city;
  18:   
  19:      public void Save(string fileName){
  20:           FileStream s=new FileStream(fileName,FileMode.Create);
  21:           SoapFormatter sf=new SoapFormatter();
  22:           sf.Serialize(s,this);
  23:      }
  24:   
  25:      static void Main(string[] args){
  26:           User u=new User();
  27:           u.userID="first";
  28:           u.password="Zxfd12Qs";
  29:           u.email=".com";
  30:           u.city="";
  31:           Validator v=new Validator();
  32:           if(!v.IsValid(u)){
  33:                foreach(string message in v.Messages)
  34:                     Console.WriteLine(message);
  35:           }
  36:           else {u.Save("user.txt");}
  37:      }
  38:  }
 
   1:  [AttributeUsage(AttributeTargets.Property|AttributeTargets.Field)]
   2:  public class ValidLengthAttribute : Attribute{
   3:      private int _min;
   4:      private int _max;
   5:      private string _message;
   6:   
   7:      public ValidLengthAttribute(int min,int max){
   8:           _min=min;
   9:           _max=max;
  10:      }
  11:   
  12:      public string Message{
  13:           get {return(_message);}
  14:           set {_message=value;}
  15:      }
  16:   
  17:      public string Min{
  18:           get{return _min.ToString();}
  19:      }
  20:   
  21:      public string Max{
  22:           get{return _max.ToString();}
  23:      }
  24:   
  25:      public bool IsValid(string theValue){
  26:           int length=theValue.Length;
  27:           if(length >= _min && length <= _max) return true;
  28:           return false;
  29:      }
  30:  }

 

   1:  public class Validator{
   2:      public ArrayList Messages=new ArrayList();
   3:   
   4:      public bool IsValid(object anObject){
   5:           bool isValid=true;
   6:           FieldInfo[] fields = anObject.GetType().GetFields(BindingFlags.Public|BindingFlags.Instance);
   7:                foreach (FieldInfo field in fields)
   8:                     if(!isValidField(field,anObject)) isValid=false;
   9:                return isValid;
  10:      }
  11:   
  12:      private bool isValidField(FieldInfo aField,object anObject){
  13:           object[] attributes=aField.GetCustomAttributes(typeof(ValidLengthAttribute),true);
  14:           if(attributes.GetLength(0) ==0) return true;
  15:           return isValidField(aField,anObject,(ValidLengthAttribute)attributes[0]);
  16:      }
  17:   
  18:      private bool isValidField(FieldInfo aField, object anObject,ValidLengthAttribute anAttr){
  19:           string theValue=(string)aField.GetValue(anObject);
  20:           if (anAttr.IsValid(theValue)) return true;
  21:           addMessages(aField,anAttr);
  22:           return false;
  23:      }
  24:   
  25:      private void addMessages(FieldInfo aField,ValidLengthAttribute anAttr){
  26:           if(anAttr.Message !=null){
  27:                Messages.Add(anAttr.Message);
  28:                return;
  29:           }
  30:           Messages.Add("Invalid range for "+aField.Name+". Valid range is between "+anAttr.Min+" and "+anAttr.Max);
  31:      }
  32:  }
 
Monday, April 21, 2008 3:35:05 PM UTC  #    Comments [0]    |  Trackback
Copyright © 2009 Kevin Mocha. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.
Pick a theme: