Skip to content

Instantly share code, notes, and snippets.

@pjvds
Created February 19, 2011 15:42
Show Gist options
  • Save pjvds/835132 to your computer and use it in GitHub Desktop.
Save pjvds/835132 to your computer and use it in GitHub Desktop.
private static long? GetKnownVersion(ICommand cmd)
{
const BindingFlags allInstance = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
var knownVersionAttributeType = typeof (KnownVersionAttribute);
var commandType = cmd.GetType();
var knownVersionProperties =
commandType.GetProperties(allInstance).Where(p => p.IsDefined(knownVersionAttributeType, false));
if (knownVersionProperties.IsEmpty())
{
return null;
}
else if (knownVersionProperties.Count() > 1)
{
throw new CommandMappingException(string.Format("Multiple {0} attributes found on command {1}.",
knownVersionAttributeType.FullName, commandType.FullName));
}
var knownVersionProperty = knownVersionProperties.Single();
if (!typeof (long).IsAssignableFrom(knownVersionProperty.PropertyType))
{
throw new CommandMappingException(
string.Format("Known version property {0} on command {1} is not of a valid type." +
"The type must be assignable to {2} and it is not.", knownVersionProperty.Name,
commandType.FullName, typeof (long).FullName));
}
long knownVersion = (long)knownVersionProperty.GetValue(cmd, null);
return knownVersion;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment