1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
//-----------------------------------------------------------------------
// <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;
using System.Xml;
using System.Globalization;
internal class MessagePart {
private static readonly Dictionary<Type, ValueMapping> converters = new Dictionary<Type, ValueMapping>();
private ValueMapping converter;
private PropertyInfo property;
private FieldInfo field;
private Type memberDeclaredType;
private object defaultMemberValue;
static MessagePart() {
Map<Uri>(uri => uri.AbsoluteUri, str => new Uri(str));
Map<DateTime>(dt => XmlConvert.ToString(dt, XmlDateTimeSerializationMode.Utc), str => XmlConvert.ToDateTime(str, XmlDateTimeSerializationMode.Utc));
}
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 ArgumentException(string.Format(
CultureInfo.CurrentCulture,
MessagingStrings.UnexpectedType,
typeof(FieldInfo).Name + ", " + typeof(PropertyInfo).Name,
member.GetType().Name), "member");
}
if (attribute == null) {
throw new ArgumentNullException("attribute");
}
this.Name = attribute.Name ?? member.Name;
this.Signed = attribute.Signed;
this.IsRequired = attribute.IsRequired;
this.memberDeclaredType = (this.field != null) ? this.field.FieldType : this.property.PropertyType;
this.defaultMemberValue = deriveDefaultValue(this.memberDeclaredType);
if (!converters.TryGetValue(this.memberDeclaredType, out this.converter)) {
this.converter = new ValueMapping(
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; }
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) {
return this.ToString(this.GetValueAsObject(message));
}
internal bool IsNondefaultValueSet(IProtocolMessage message) {
if (this.memberDeclaredType.IsValueType) {
return !GetValueAsObject(message).Equals(this.defaultMemberValue);
} else {
return this.defaultMemberValue != GetValueAsObject(message);
}
}
internal bool IsValidValue(IProtocolMessage message) {
return true;
}
private static object deriveDefaultValue(Type type) {
if (type.IsValueType) {
return Activator.CreateInstance(type);
} else {
return null;
}
}
private object GetValueAsObject(IProtocolMessage message) {
if (this.property != null) {
return this.property.GetValue(message, null);
} else {
return this.field.GetValue(message);
}
}
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) : 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;
}
}
}
|