diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2008-09-17 17:49:14 -0700 |
---|---|---|
committer | Andrew <andrewarnott@gmail.com> | 2008-09-17 17:49:14 -0700 |
commit | ea7cae52f40770d1023362dadd234b8038d9e68b (patch) | |
tree | 2bf9a9bfa01de5887be1336f4a9ece33cbf171e3 /src/DotNetOAuth/Messaging/Reflection/MessagePart.cs | |
parent | 2dcfb4843722237aa214294b59ed9be782ea0cec (diff) | |
download | DotNetOpenAuth-ea7cae52f40770d1023362dadd234b8038d9e68b.zip DotNetOpenAuth-ea7cae52f40770d1023362dadd234b8038d9e68b.tar.gz DotNetOpenAuth-ea7cae52f40770d1023362dadd234b8038d9e68b.tar.bz2 |
Added MessageDictionary and supporting classes and tests.
Very little documentation is there until I can prove the idea works to solve our signing design problem.
Diffstat (limited to 'src/DotNetOAuth/Messaging/Reflection/MessagePart.cs')
-rw-r--r-- | src/DotNetOAuth/Messaging/Reflection/MessagePart.cs | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/DotNetOAuth/Messaging/Reflection/MessagePart.cs b/src/DotNetOAuth/Messaging/Reflection/MessagePart.cs new file mode 100644 index 0000000..09f959b --- /dev/null +++ b/src/DotNetOAuth/Messaging/Reflection/MessagePart.cs @@ -0,0 +1,91 @@ +//-----------------------------------------------------------------------
+// <copyright file="MessagePart.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth.Messaging.Reflection {
+ using System;
+ using System.Collections.Generic;
+ using System.Net.Security;
+ using System.Reflection;
+
+ internal class MessagePart {
+ private static readonly Dictionary<Type, ValueMapping> converters = new Dictionary<Type, ValueMapping>();
+
+ private ValueMapping converter;
+
+ private PropertyInfo property;
+
+ private FieldInfo field;
+
+ static MessagePart() {
+ Map<Uri>(uri => uri.AbsoluteUri, str => new Uri(str));
+ }
+
+ internal MessagePart(MemberInfo member, MessagePartAttribute attribute) {
+ if (member == null) {
+ throw new ArgumentNullException("member");
+ }
+
+ this.field = member as FieldInfo;
+ this.property = member as PropertyInfo;
+ if (this.field == null && this.property == null) {
+ throw new ArgumentOutOfRangeException("member"); // TODO: add descriptive message
+ }
+
+ if (attribute == null) {
+ throw new ArgumentNullException("attribute");
+ }
+
+ this.Name = attribute.Name ?? member.Name;
+ this.Signed = attribute.Signed;
+ this.IsRequired = !attribute.Optional;
+
+ if (!converters.TryGetValue(member.DeclaringType, out this.converter)) {
+ Type memberDeclaredType = (this.field != null) ? this.field.FieldType : this.property.PropertyType;
+ this.converter = new ValueMapping(
+ obj => obj != null ? obj.ToString() : null,
+ str => str != null ? Convert.ChangeType(str, memberDeclaredType) : null);
+ }
+ }
+
+ internal string Name { get; set; }
+
+ internal ProtectionLevel Signed { get; set; }
+
+ internal bool IsRequired { get; set; }
+
+ internal object ToValue(string value) {
+ return this.converter.StringToValue(value);
+ }
+
+ internal string ToString(object value) {
+ return this.converter.ValueToString(value);
+ }
+
+ internal void SetValue(IProtocolMessage message, string value) {
+ if (this.property != null) {
+ this.property.SetValue(message, this.ToValue(value), null);
+ } else {
+ this.field.SetValue(message, this.ToValue(value));
+ }
+ }
+
+ internal string GetValue(IProtocolMessage message) {
+ if (this.property != null) {
+ return this.ToString(this.property.GetValue(message, null));
+ } else {
+ return this.ToString(this.field.GetValue(message));
+ }
+ }
+
+ private static void Map<T>(Func<T, string> toString, Func<string, T> toValue) where T : class {
+ converters.Add(
+ typeof(T),
+ new ValueMapping(
+ obj => obj != null ? toString((T)obj) : null,
+ str => str != null ? toValue(str) : null));
+ }
+ }
+}
|