Created
February 19, 2011 15:42
-
-
Save pjvds/835132 to your computer and use it in GitHub Desktop.
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
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