Skip to content

Instantly share code, notes, and snippets.

@keithbloom
Created January 26, 2012 16:05
Show Gist options
  • Save keithbloom/1683502 to your computer and use it in GitHub Desktop.
Save keithbloom/1683502 to your computer and use it in GitHub Desktop.
Using delegates to clean up multiple IF statements
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