//----------------------------------------------------------------------- // // 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.Contracts; using System.Linq; using System.Reflection; /// /// A design-time helper to allow controls to have properties /// of type . /// public class UriConverter : ConverterBase { /// /// Initializes a new instance of the UriConverter class. /// protected UriConverter() { } /// /// Gets the type to reflect over to extract the well known values. /// protected virtual Type WellKnownValuesType { get { return null; } } /// /// Returns whether the given value object is valid for this type and for the specified context. /// /// An that provides a format context. /// The to test for validity. /// /// true if the specified value is valid for this object; otherwise, false. /// public override bool IsValid(ITypeDescriptorContext context, object value) { Uri uriValue; string stringValue; if ((uriValue = value as Uri) != null) { return uriValue.IsAbsoluteUri; } else if ((stringValue = value as string) != null) { Uri result; return stringValue.Length == 0 || Uri.TryCreate(stringValue, UriKind.Absolute, out result); } else { return false; } } /// /// Converts a value from its string representation to its strongly-typed object. /// /// The value. /// The strongly-typed object. [Pure] protected override Uri ConvertFrom(string value) { return string.IsNullOrEmpty(value) ? null : new Uri(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 override InstanceDescriptor CreateFrom(Uri value) { if (value == null) { return null; } MemberInfo uriCtor = typeof(Uri).GetConstructor(new Type[] { typeof(string) }); return CreateInstanceDescriptor(uriCtor, new object[] { value.AbsoluteUri }); } /// /// Converts the strongly-typed value to a string. /// /// The value to convert. /// The string representation of the object. [Pure] protected override string ConvertToString(Uri value) { if (value == null) { return null; } return value.AbsoluteUri; } /// /// Gets the standard claim type URIs known to the library. /// /// An array of the standard claim types. [Pure] protected override ICollection GetStandardValuesForCache() { if (this.WellKnownValuesType != null) { var fields = from field in this.WellKnownValuesType.GetFields(BindingFlags.Static | BindingFlags.Public) let value = (string)field.GetValue(null) where value != null select new Uri(value); var properties = from prop in this.WellKnownValuesType.GetProperties(BindingFlags.Static | BindingFlags.Public) let value = (string)prop.GetValue(null, null) where value != null select new Uri(value); return fields.Concat(properties).ToArray(); } else { return new Uri[0]; } } } }