diff options
Diffstat (limited to 'src/DotNetOpenAuth.Core/Messaging/Reflection')
7 files changed, 18 insertions, 69 deletions
diff --git a/src/DotNetOpenAuth.Core/Messaging/Reflection/DefaultEncoderAttribute.cs b/src/DotNetOpenAuth.Core/Messaging/Reflection/DefaultEncoderAttribute.cs index d827972..adf0f33 100644 --- a/src/DotNetOpenAuth.Core/Messaging/Reflection/DefaultEncoderAttribute.cs +++ b/src/DotNetOpenAuth.Core/Messaging/Reflection/DefaultEncoderAttribute.cs @@ -9,6 +9,7 @@ namespace DotNetOpenAuth.Messaging.Reflection { using System.Collections.Generic; using System.Linq; using System.Text; + using Validation; /// <summary> /// Allows a custom class or struct to be serializable between itself and a string representation. @@ -21,7 +22,7 @@ namespace DotNetOpenAuth.Messaging.Reflection { /// <param name="converterType">The <see cref="IMessagePartEncoder"/> implementing type to use for serializing this type.</param> public DefaultEncoderAttribute(Type converterType) { Requires.NotNull(converterType, "converterType"); - Requires.True(typeof(IMessagePartEncoder).IsAssignableFrom(converterType), "Argument must be a type that implements {0}.", typeof(IMessagePartEncoder).Name); + Requires.That(typeof(IMessagePartEncoder).IsAssignableFrom(converterType), "Argument must be a type that implements {0}.", typeof(IMessagePartEncoder).Name); this.Encoder = (IMessagePartEncoder)Activator.CreateInstance(converterType); } diff --git a/src/DotNetOpenAuth.Core/Messaging/Reflection/IMessagePartEncoder.cs b/src/DotNetOpenAuth.Core/Messaging/Reflection/IMessagePartEncoder.cs index 6186cd7..017c7d7 100644 --- a/src/DotNetOpenAuth.Core/Messaging/Reflection/IMessagePartEncoder.cs +++ b/src/DotNetOpenAuth.Core/Messaging/Reflection/IMessagePartEncoder.cs @@ -7,9 +7,9 @@ namespace DotNetOpenAuth.Messaging.Reflection { using System; using System.Collections.Generic; - using System.Diagnostics.Contracts; using System.Linq; using System.Text; + using Validation; /// <summary> /// An interface describing how various objects can be serialized and deserialized between their object and string forms. @@ -17,7 +17,6 @@ namespace DotNetOpenAuth.Messaging.Reflection { /// <remarks> /// Implementations of this interface must include a default constructor and must be thread-safe. /// </remarks> - [ContractClass(typeof(IMessagePartEncoderContract))] public interface IMessagePartEncoder { /// <summary> /// Encodes the specified value. @@ -34,45 +33,4 @@ namespace DotNetOpenAuth.Messaging.Reflection { /// <exception cref="FormatException">Thrown when the string value given cannot be decoded into the required object type.</exception> object Decode(string value); } - - /// <summary> - /// Code contract for the <see cref="IMessagePartEncoder"/> type. - /// </summary> - [ContractClassFor(typeof(IMessagePartEncoder))] - internal abstract class IMessagePartEncoderContract : IMessagePartEncoder { - /// <summary> - /// Initializes a new instance of the <see cref="IMessagePartEncoderContract"/> class. - /// </summary> - protected IMessagePartEncoderContract() { - } - - #region IMessagePartEncoder Members - - /// <summary> - /// Encodes the specified value. - /// </summary> - /// <param name="value">The value. Guaranteed to never be null.</param> - /// <returns> - /// The <paramref name="value"/> in string form, ready for message transport. - /// </returns> - string IMessagePartEncoder.Encode(object value) { - Requires.NotNull(value, "value"); - throw new NotImplementedException(); - } - - /// <summary> - /// Decodes the specified value. - /// </summary> - /// <param name="value">The string value carried by the transport. Guaranteed to never be null, although it may be empty.</param> - /// <returns> - /// The deserialized form of the given string. - /// </returns> - /// <exception cref="FormatException">Thrown when the string value given cannot be decoded into the required object type.</exception> - object IMessagePartEncoder.Decode(string value) { - Requires.NotNull(value, "value"); - throw new NotImplementedException(); - } - - #endregion - } } diff --git a/src/DotNetOpenAuth.Core/Messaging/Reflection/MessageDescription.cs b/src/DotNetOpenAuth.Core/Messaging/Reflection/MessageDescription.cs index 7e67842..cd04e1d 100644 --- a/src/DotNetOpenAuth.Core/Messaging/Reflection/MessageDescription.cs +++ b/src/DotNetOpenAuth.Core/Messaging/Reflection/MessageDescription.cs @@ -12,6 +12,7 @@ namespace DotNetOpenAuth.Messaging.Reflection { using System.Globalization; using System.Linq; using System.Reflection; + using Validation; /// <summary> /// A mapping between serialized key names and <see cref="MessagePart"/> instances describing @@ -30,7 +31,7 @@ namespace DotNetOpenAuth.Messaging.Reflection { /// <param name="messageType">Type of the message.</param> /// <param name="messageVersion">The message version.</param> internal MessageDescription(Type messageType, Version messageVersion) { - Requires.NotNullSubtype<IMessage>(messageType, "messageType"); + RequiresEx.NotNullSubtype<IMessage>(messageType, "messageType"); Requires.NotNull(messageVersion, "messageVersion"); this.MessageType = messageType; @@ -80,7 +81,6 @@ namespace DotNetOpenAuth.Messaging.Reflection { [Pure] internal MessageDictionary GetDictionary(IMessage message) { Requires.NotNull(message, "message"); - Contract.Ensures(Contract.Result<MessageDictionary>() != null); return this.GetDictionary(message, false); } @@ -93,7 +93,6 @@ namespace DotNetOpenAuth.Messaging.Reflection { [Pure] internal MessageDictionary GetDictionary(IMessage message, bool getOriginalValues) { Requires.NotNull(message, "message"); - Contract.Ensures(Contract.Result<MessageDictionary>() != null); return new MessageDictionary(message, this, getOriginalValues); } diff --git a/src/DotNetOpenAuth.Core/Messaging/Reflection/MessageDescriptionCollection.cs b/src/DotNetOpenAuth.Core/Messaging/Reflection/MessageDescriptionCollection.cs index 3517abc..f27a7af 100644 --- a/src/DotNetOpenAuth.Core/Messaging/Reflection/MessageDescriptionCollection.cs +++ b/src/DotNetOpenAuth.Core/Messaging/Reflection/MessageDescriptionCollection.cs @@ -10,11 +10,11 @@ namespace DotNetOpenAuth.Messaging.Reflection { using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Linq; + using Validation; /// <summary> /// A cache of <see cref="MessageDescription"/> instances. /// </summary> - [ContractVerification(true)] internal class MessageDescriptionCollection : IEnumerable<MessageDescription> { /// <summary> /// A dictionary of reflected message types and the generated reflection information. @@ -68,9 +68,8 @@ namespace DotNetOpenAuth.Messaging.Reflection { [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "System.Diagnostics.Contracts.__ContractsRuntime.Assume(System.Boolean,System.String,System.String)", Justification = "No localization required.")] [Pure] internal MessageDescription Get(Type messageType, Version messageVersion) { - Requires.NotNullSubtype<IMessage>(messageType, "messageType"); + RequiresEx.NotNullSubtype<IMessage>(messageType, "messageType"); Requires.NotNull(messageVersion, "messageVersion"); - Contract.Ensures(Contract.Result<MessageDescription>() != null); MessageTypeAndVersion key = new MessageTypeAndVersion(messageType, messageVersion); @@ -106,7 +105,6 @@ namespace DotNetOpenAuth.Messaging.Reflection { [Pure] internal MessageDescription Get(IMessage message) { Requires.NotNull(message, "message"); - Contract.Ensures(Contract.Result<MessageDescription>() != null); return this.Get(message.GetType(), message.Version); } @@ -136,8 +134,7 @@ namespace DotNetOpenAuth.Messaging.Reflection { /// <summary> /// A struct used as the key to bundle message type and version. /// </summary> - [ContractVerification(true)] - private struct MessageTypeAndVersion { + private struct MessageTypeAndVersion { /// <summary> /// Backing store for the <see cref="Type"/> property. /// </summary> diff --git a/src/DotNetOpenAuth.Core/Messaging/Reflection/MessageDictionary.cs b/src/DotNetOpenAuth.Core/Messaging/Reflection/MessageDictionary.cs index cf44863..a2dddb2 100644 --- a/src/DotNetOpenAuth.Core/Messaging/Reflection/MessageDictionary.cs +++ b/src/DotNetOpenAuth.Core/Messaging/Reflection/MessageDictionary.cs @@ -11,13 +11,13 @@ namespace DotNetOpenAuth.Messaging.Reflection { using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; + using Validation; /// <summary> /// Wraps an <see cref="IMessage"/> instance in a dictionary that /// provides access to both well-defined message properties and "extra" /// name/value pairs that have no properties associated with them. /// </summary> - [ContractVerification(false)] internal class MessageDictionary : IDictionary<string, string> { /// <summary> /// The <see cref="IMessage"/> instance manipulated by this dictionary. @@ -55,7 +55,6 @@ namespace DotNetOpenAuth.Messaging.Reflection { /// </summary> public IMessage Message { get { - Contract.Ensures(Contract.Result<IMessage>() != null); return this.message; } } @@ -65,7 +64,6 @@ namespace DotNetOpenAuth.Messaging.Reflection { /// </summary> public MessageDescription Description { get { - Contract.Ensures(Contract.Result<MessageDescription>() != null); return this.description; } } @@ -380,7 +378,6 @@ namespace DotNetOpenAuth.Messaging.Reflection { /// <returns>The generated dictionary.</returns> [Pure] public IDictionary<string, string> Serialize() { - Contract.Ensures(Contract.Result<IDictionary<string, string>>() != null); return this.Serializer.Serialize(this); } diff --git a/src/DotNetOpenAuth.Core/Messaging/Reflection/MessagePart.cs b/src/DotNetOpenAuth.Core/Messaging/Reflection/MessagePart.cs index a6e8da2..add4beb 100644 --- a/src/DotNetOpenAuth.Core/Messaging/Reflection/MessagePart.cs +++ b/src/DotNetOpenAuth.Core/Messaging/Reflection/MessagePart.cs @@ -9,18 +9,17 @@ namespace DotNetOpenAuth.Messaging.Reflection { using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; - using System.Diagnostics.Contracts; using System.Globalization; using System.Linq; using System.Net.Security; using System.Reflection; using System.Xml; using DotNetOpenAuth.Configuration; + using Validation; /// <summary> /// Describes an individual member of a message and assists in its serialization. /// </summary> - [ContractVerification(true)] [DebuggerDisplay("MessagePart {Name}")] internal class MessagePart { /// <summary> @@ -66,20 +65,20 @@ namespace DotNetOpenAuth.Messaging.Reflection { [SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline", Justification = "Much more efficient initialization when we can call methods.")] static MessagePart() { Func<string, Uri> safeUri = str => { - Contract.Assume(str != null); + Assumes.True(str != null); return new Uri(str); }; Func<string, bool> safeBool = str => { - Contract.Assume(str != null); + Assumes.True(str != null); return bool.Parse(str); }; Func<byte[], string> safeFromByteArray = bytes => { - Contract.Assume(bytes != null); + Assumes.True(bytes != null); return Convert.ToBase64String(bytes); }; Func<string, byte[]> safeToByteArray = str => { - Contract.Assume(str != null); + Assumes.True(str != null); return Convert.FromBase64String(str); }; Map<Uri>(uri => uri.AbsoluteUri, uri => uri.OriginalString, safeUri); @@ -106,7 +105,7 @@ namespace DotNetOpenAuth.Messaging.Reflection { [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "Unavoidable"), SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily", Justification = "Code contracts requires it.")] internal MessagePart(MemberInfo member, MessagePartAttribute attribute) { Requires.NotNull(member, "member"); - Requires.True(member is FieldInfo || member is PropertyInfo, "member"); + Requires.That(member is FieldInfo || member is PropertyInfo, "member", "Member must be a property or field."); Requires.NotNull(attribute, "attribute"); this.field = member as FieldInfo; @@ -119,7 +118,7 @@ namespace DotNetOpenAuth.Messaging.Reflection { this.memberDeclaredType = (this.field != null) ? this.field.FieldType : this.property.PropertyType; this.defaultMemberValue = DeriveDefaultValue(this.memberDeclaredType); - Contract.Assume(this.memberDeclaredType != null); // CC missing PropertyInfo.PropertyType ensures result != null + Assumes.True(this.memberDeclaredType != null); // CC missing PropertyInfo.PropertyType ensures result != null if (attribute.Encoder == null) { if (!converters.TryGetValue(this.memberDeclaredType, out this.converter)) { if (this.memberDeclaredType.IsGenericType && @@ -203,7 +202,7 @@ namespace DotNetOpenAuth.Messaging.Reflection { /// </summary> internal string StaticConstantValue { get { - Requires.ValidState(this.IsConstantValueAvailableStatically); + RequiresEx.ValidState(this.IsConstantValueAvailableStatically); return this.ToString(this.field.GetValue(null), false); } } @@ -394,7 +393,6 @@ namespace DotNetOpenAuth.Messaging.Reflection { /// <returns>An instance of the desired encoder.</returns> private static IMessagePartEncoder GetEncoder(Type messagePartEncoder) { Requires.NotNull(messagePartEncoder, "messagePartEncoder"); - Contract.Ensures(Contract.Result<IMessagePartEncoder>() != null); IMessagePartEncoder encoder; lock (encoders) { diff --git a/src/DotNetOpenAuth.Core/Messaging/Reflection/ValueMapping.cs b/src/DotNetOpenAuth.Core/Messaging/Reflection/ValueMapping.cs index 4139f52..c45eb5d 100644 --- a/src/DotNetOpenAuth.Core/Messaging/Reflection/ValueMapping.cs +++ b/src/DotNetOpenAuth.Core/Messaging/Reflection/ValueMapping.cs @@ -6,12 +6,11 @@ namespace DotNetOpenAuth.Messaging.Reflection { using System; - using System.Diagnostics.Contracts; + using Validation; /// <summary> /// A pair of conversion functions to map some type to a string and back again. /// </summary> - [ContractVerification(true)] internal struct ValueMapping { /// <summary> /// The mapping function that converts some custom type to a string. |