Created
January 26, 2012 16:05
-
-
Save keithbloom/1683502 to your computer and use it in GitHub Desktop.
Using delegates to clean up multiple IF statements
This file contains hidden or 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
private void RunValidation() | |
{ | |
foreach (var propertyInfo in GetType().GetProperties()) | |
{ | |
var attribute = Attribute.GetCustomAttribute(propertyInfo, typeof(RequiredField)) as RequiredField; | |
if (attribute == null) continue; | |
/* | |
if (propertyInfo.PropertyType == typeof(string) | |
&& string.IsNullOrEmpty((string)propertyInfo.GetValue(this, null))) | |
{ | |
AddValidationError(propertyInfo); | |
} | |
if (propertyInfo.PropertyType == typeof(Guid) | |
&& (Guid)propertyInfo.GetValue(this, null) == Guid.Empty) | |
{ | |
AddValidationError(propertyInfo); | |
} | |
*/ | |
CheckType<string>(propertyInfo, typeof(string), String.IsNullOrEmpty); | |
CheckType<Guid>(propertyInfo, typeof(Guid), x => x == Guid.Empty); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment