Created
June 22, 2011 16:15
-
-
Save waldyrfelix/1040458 to your computer and use it in GitHub Desktop.
Exemplo de DataAnnotation próprio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel; | |
using System.ComponentModel.DataAnnotations; | |
namespace AppWeb.Attribute | |
{ | |
[AttributeUsage(AttributeTargets.Class)] | |
public class ComparaPropriedadesAttribute : ValidationAttribute | |
{ | |
public string PrimeiroCampo { get; private set; } | |
public string SegundoCampo { get; private set; } | |
public ComparaPropriedadesAttribute(string primeiroCampo, string segundoCampo) | |
{ | |
this.PrimeiroCampo = primeiroCampo; | |
this.SegundoCampo = segundoCampo; | |
} | |
public override bool IsValid(object value) | |
{ | |
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value); | |
object primeiroValor = properties.Find(PrimeiroCampo, true).GetValue(value); | |
object segundoValor = properties.Find(SegundoCampo, true).GetValue(value); | |
if (primeiroValor == null || segundoValor == null) | |
{ | |
return false; | |
} | |
return primeiroValor.Equals(segundoValor); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment