Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 29th, 2012  |  syntax: None  |  size: 1.05 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.ComponentModel;
  3. using System.ComponentModel.DataAnnotations;
  4.  
  5. namespace AppWeb.Attribute
  6. {
  7.     [AttributeUsage(AttributeTargets.Class)]
  8.     public class ComparaPropriedadesAttribute : ValidationAttribute
  9.     {
  10.         public string PrimeiroCampo { get; private set; }
  11.         public string SegundoCampo { get; private set; }
  12.  
  13.         public ComparaPropriedadesAttribute(string primeiroCampo, string segundoCampo)
  14.         {
  15.             this.PrimeiroCampo = primeiroCampo;
  16.             this.SegundoCampo = segundoCampo;
  17.         }
  18.  
  19.         public override bool IsValid(object value)
  20.         {
  21.             PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
  22.  
  23.             object primeiroValor = properties.Find(PrimeiroCampo, true).GetValue(value);
  24.             object segundoValor = properties.Find(SegundoCampo, true).GetValue(value);
  25.  
  26.             if (primeiroValor == null || segundoValor == null)
  27.             {
  28.                 return false;
  29.             }
  30.  
  31.             return primeiroValor.Equals(segundoValor);
  32.         }
  33.     }
  34. }