diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2008-09-18 16:17:42 -0700 |
---|---|---|
committer | Andrew <andrewarnott@gmail.com> | 2008-09-18 16:17:42 -0700 |
commit | 11615a19bad90741c7a755eaf741b8683992c9ff (patch) | |
tree | 5ad7b033a81cf74192e31d496bdeb76401a6af44 /src/DotNetOAuth/Messaging/Reflection/MessagePart.cs | |
parent | 95e418aa02296a93587c4e07b0f3eec8cd379416 (diff) | |
download | DotNetOpenAuth-11615a19bad90741c7a755eaf741b8683992c9ff.zip DotNetOpenAuth-11615a19bad90741c7a755eaf741b8683992c9ff.tar.gz DotNetOpenAuth-11615a19bad90741c7a755eaf741b8683992c9ff.tar.bz2 |
Fixed a few bugs and got all tests passing.
Diffstat (limited to 'src/DotNetOAuth/Messaging/Reflection/MessagePart.cs')
-rw-r--r-- | src/DotNetOAuth/Messaging/Reflection/MessagePart.cs | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/src/DotNetOAuth/Messaging/Reflection/MessagePart.cs b/src/DotNetOAuth/Messaging/Reflection/MessagePart.cs index 2005370..b3c66d0 100644 --- a/src/DotNetOAuth/Messaging/Reflection/MessagePart.cs +++ b/src/DotNetOAuth/Messaging/Reflection/MessagePart.cs @@ -9,6 +9,8 @@ namespace DotNetOAuth.Messaging.Reflection { using System.Collections.Generic;
using System.Net.Security;
using System.Reflection;
+ using System.Xml;
+ using System.Globalization;
internal class MessagePart {
private static readonly Dictionary<Type, ValueMapping> converters = new Dictionary<Type, ValueMapping>();
@@ -25,6 +27,7 @@ namespace DotNetOAuth.Messaging.Reflection { static MessagePart() {
Map<Uri>(uri => uri.AbsoluteUri, str => new Uri(str));
+ Map<DateTime>(dt => XmlConvert.ToString(dt, XmlDateTimeSerializationMode.Utc), str => DateTime.Parse(str));
}
internal MessagePart(MemberInfo member, MessagePartAttribute attribute) {
@@ -53,6 +56,9 @@ namespace DotNetOAuth.Messaging.Reflection { obj => obj != null ? obj.ToString() : null,
str => str != null ? Convert.ChangeType(str, memberDeclaredType) : null);
}
+
+ // Validate a sane combination of settings
+ ValidateSettings();
}
internal string Name { get; set; }
@@ -109,12 +115,34 @@ namespace DotNetOAuth.Messaging.Reflection { }
}
- private static void Map<T>(Func<T, string> toString, Func<string, T> toValue) where T : class {
+ private static void Map<T>(Func<T, string> toString, Func<string, T> toValue) {
converters.Add(
typeof(T),
new ValueMapping(
obj => obj != null ? toString((T)obj) : null,
- str => str != null ? toValue(str) : null));
+ str => str != null ? toValue(str) : default(T)));
+ }
+
+ private void ValidateSettings() {
+ // An optional tag on a non-nullable value type is a contradiction.
+ if (!this.IsRequired && IsNonNullableValueType(this.memberDeclaredType)) {
+ MemberInfo member = (MemberInfo)this.field ?? this.property;
+ throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
+ "Invalid combination: {0} on message type {1} is a non-nullable value type but is marked as optional.",
+ member.Name, member.DeclaringType));
+ }
+ }
+
+ private static bool IsNonNullableValueType(Type type) {
+ if (!type.IsValueType) {
+ return false;
+ }
+
+ if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) {
+ return false;
+ }
+
+ return true;
}
}
}
|