//-----------------------------------------------------------------------
//
// Copyright (c) Andrew Arnott. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOAuth.Messaging.Reflection {
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
///
/// A mapping between serialized key names and instances describing
/// those key/values pairs.
///
internal class MessageDescription {
///
/// A dictionary of reflected message types and the generated reflection information.
///
private static Dictionary reflectedMessageTypes = new Dictionary();
///
/// The type of message this instance was generated from.
///
private Type messageType;
///
/// A mapping between the serialized key names and their
/// describing instances.
///
private Dictionary mapping;
///
/// Initializes a new instance of the class.
///
/// The type of message to reflect over.
private MessageDescription(Type messageType) {
Debug.Assert(messageType != null, "messageType == null");
if (!typeof(IProtocolMessage).IsAssignableFrom(messageType)) {
throw new ArgumentException(string.Format(
CultureInfo.CurrentCulture,
MessagingStrings.UnexpectedType,
typeof(IProtocolMessage),
messageType));
}
this.messageType = messageType;
this.ReflectMessageType();
}
///
/// Gets the mapping between the serialized key names and their describing
/// instances.
///
internal IDictionary Mapping {
get { return this.mapping; }
}
///
/// Gets a instance prepared for the
/// given message type.
///
/// A type that implements .
/// A instance.
internal static MessageDescription Get(Type messageType) {
if (messageType == null) {
throw new ArgumentNullException("messageType");
}
MessageDescription result;
if (!reflectedMessageTypes.TryGetValue(messageType, out result)) {
lock (reflectedMessageTypes) {
if (!reflectedMessageTypes.TryGetValue(messageType, out result)) {
reflectedMessageTypes[messageType] = result = new MessageDescription(messageType);
}
}
}
return result;
}
///
/// Reflects over some -implementing type
/// and prepares to serialize/deserialize instances of that type.
///
internal void ReflectMessageType() {
this.mapping = new Dictionary();
Type currentType = this.messageType;
do {
foreach (MemberInfo member in currentType.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)) {
if (member is PropertyInfo || member is FieldInfo) {
MessagePartAttribute partAttribute = member.GetCustomAttributes(typeof(MessagePartAttribute), true).OfType().FirstOrDefault();
if (partAttribute != null) {
MessagePart part = new MessagePart(member, partAttribute);
this.mapping.Add(part.Name, part);
}
}
}
currentType = currentType.BaseType;
} while (currentType != null);
}
///
/// Verifies that a given set of keys include all the required parameters
/// for this message type or throws an exception.
///
/// The names of all parameters included in a message.
/// Thrown when required parts of a message are not in
internal void EnsureRequiredMessagePartsArePresent(IEnumerable keys) {
var missingKeys = (from part in Mapping.Values
where part.IsRequired && !keys.Contains(part.Name)
select part.Name).ToArray();
if (missingKeys.Length > 0) {
throw new ProtocolException(
string.Format(
CultureInfo.CurrentCulture,
MessagingStrings.RequiredParametersMissing,
this.messageType.FullName,
string.Join(", ", missingKeys)));
}
}
}
}