//----------------------------------------------------------------------- // // Copyright (c) Outercurve Foundation. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOpenAuth.ComponentModel { using System; using System.Collections; using System.ComponentModel; using System.ComponentModel.Design.Serialization; using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Globalization; using System.Reflection; using System.Security; using System.Security.Permissions; /// /// A design-time helper to allow Intellisense to aid typing /// ClaimType URIs. /// /// The strong-type of the property this class is affixed to. public abstract class ConverterBase : TypeConverter { /// /// A cache of the standard claim types known to the application. /// private StandardValuesCollection standardValues; /// /// Initializes a new instance of the ConverterBase class. /// protected ConverterBase() { } /// /// Gets a cache of the standard values to suggest. /// private StandardValuesCollection StandardValueCache { get { if (this.standardValues == null) { this.standardValues = new StandardValuesCollection(this.GetStandardValuesForCache()); } return this.standardValues; } } /// /// Returns whether this object supports a standard set of values that can be picked from a list, using the specified context. /// /// An that provides a format context. /// /// true if should be called to find a common set of values the object supports; otherwise, false. /// public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { return this.StandardValueCache.Count > 0; } /// /// Returns a collection of standard values for the data type this type converter is designed for when provided with a format context. /// /// An that provides a format context that can be used to extract additional information about the environment from which this converter is invoked. This parameter or properties of this parameter can be null. /// /// A that holds a standard set of valid values, or null if the data type does not support a standard set of values. /// public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { return this.StandardValueCache; } /// /// Returns whether the collection of standard values returned from is an exclusive list of possible values, using the specified context. /// /// An that provides a format context. /// /// true if the returned from is an exhaustive list of possible values; false if other values are possible. /// public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) { return false; } /// /// Returns whether this converter can convert an object of the given type to the type of this converter, using the specified context. /// /// An that provides a format context. /// A that represents the type you want to convert from. /// /// true if this converter can perform the conversion; otherwise, false. /// public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); } /// /// Returns whether this converter can convert the object to the specified type, using the specified context. /// /// An that provides a format context. /// A that represents the type you want to convert to. /// /// true if this converter can perform the conversion; otherwise, false. /// public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { return destinationType == typeof(string) || destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType); } /// /// Converts the given object to the type of this converter, using the specified context and culture information. /// /// An that provides a format context. /// The to use as the current culture. /// The to convert. /// /// An that represents the converted value. /// /// /// The conversion cannot be performed. /// public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { string stringValue = value as string; if (stringValue != null) { return this.ConvertFrom(stringValue); } else { return base.ConvertFrom(context, culture, value); } } /// /// Converts the given value object to the specified type, using the specified context and culture information. /// /// An that provides a format context. /// A . If null is passed, the current culture is assumed. /// The to convert. /// The to convert the parameter to. /// /// An that represents the converted value. /// /// /// The parameter is null. /// /// /// The conversion cannot be performed. /// [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.")] public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { Assumes.True(destinationType != null, "Missing contract."); if (destinationType.IsInstanceOfType(value)) { return value; } T typedValue = (T)value; if (destinationType == typeof(string)) { return this.ConvertToString(typedValue); } else if (destinationType == typeof(InstanceDescriptor)) { return this.CreateFrom(typedValue); } else { return base.ConvertTo(context, culture, value, destinationType); } } /// /// Creates an instance, protecting against the LinkDemand. /// /// The member info. /// The arguments. /// A , or null if sufficient permissions are unavailable. protected static InstanceDescriptor CreateInstanceDescriptor(MemberInfo memberInfo, ICollection arguments) { try { return CreateInstanceDescriptorPrivate(memberInfo, arguments); } catch (SecurityException) { return null; } } /// /// Gets the standard values to suggest with Intellisense in the designer. /// /// A collection of the standard values. [Pure] [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Potentially expensive call.")] protected virtual ICollection GetStandardValuesForCache() { return new T[0]; } /// /// Converts a value from its string representation to its strongly-typed object. /// /// The value. /// The strongly-typed object. [Pure] protected abstract T ConvertFrom(string value); /// /// Creates the reflection instructions for recreating an instance later. /// /// The value to recreate later. /// The description of how to recreate an instance. [Pure] protected abstract InstanceDescriptor CreateFrom(T value); /// /// Converts the strongly-typed value to a string. /// /// The value to convert. /// The string representation of the object. [Pure] protected abstract string ConvertToString(T value); /// /// Creates an instance, protecting against the LinkDemand. /// /// The member info. /// The arguments. /// A . [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] private static InstanceDescriptor CreateInstanceDescriptorPrivate(MemberInfo memberInfo, ICollection arguments) { return new InstanceDescriptor(memberInfo, arguments); } } }