The Labs \ Source Viewer \ SSCLI \ System \ UriTypeConverter

  1. //------------------------------------------------------------------------------
  2. // <copyright file="_UriTypeConverter.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. //
  16. // A default TypeConverter implementation for the System.Uri type
  17. //
  18. namespace System
  19. {
  20.     using System.Globalization;
  21.     using System.ComponentModel;
  22.     using System.ComponentModel.Design.Serialization;
  23.     using System.Reflection;
  24.    
  25.     //
  26.     // A limited conversion is implemented such as to and from string
  27.     // A conversion to InstanceDescriptor is also provided for design time support.
  28.     //
  29.     public class UriTypeConverter : TypeConverter
  30.     {
  31.         private UriKind m_UriKind;
  32.        
  33.        
  34.         public UriTypeConverter() : this(UriKind.RelativeOrAbsolute)
  35.         {
  36.         }
  37.        
  38.         internal UriTypeConverter(UriKind uriKind)
  39.         {
  40.             m_UriKind = uriKind;
  41.         }
  42.        
  43.        
  44.         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  45.         {
  46.             if (sourceType == null)
  47.                 throw new ArgumentNullException("sourceType");
  48.            
  49.             if (sourceType == typeof(string))
  50.                 return true;
  51.            
  52.             if (typeof(Uri).IsAssignableFrom(sourceType))
  53.                 return true;
  54.            
  55.             return base.CanConvertFrom(context, sourceType);
  56.         }
  57.         //
  58.         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  59.         {
  60.             if (destinationType == typeof(InstanceDescriptor))
  61.                 return true;
  62.            
  63.             if (destinationType == typeof(string))
  64.                 return true;
  65.            
  66.             if (destinationType == typeof(Uri))
  67.                 return true;
  68.            
  69.             return base.CanConvertTo(context, destinationType);
  70.         }
  71.         //
  72.         public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  73.         {
  74.             string uriString = value as string;
  75.             if (uriString != null)
  76.                 return new Uri(uriString, m_UriKind);
  77.            
  78.             Uri uri = value as Uri;
  79.             if (uri != null)
  80.                 return new Uri(uri.OriginalString, m_UriKind == UriKind.RelativeOrAbsolute ? uri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative : m_UriKind);
  81.            
  82.             return base.ConvertFrom(context, culture, value);
  83.         }
  84.         //
  85.         public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  86.         {
  87.             Uri uri = value as Uri;
  88.            
  89.             if (uri != null && destinationType == typeof(InstanceDescriptor)) {
  90.                 ConstructorInfo ci = typeof(Uri).GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new Type[] {typeof(string), typeof(UriKind)}, null);
  91.                 return new InstanceDescriptor(ci, new object[] {uri.OriginalString, m_UriKind == UriKind.RelativeOrAbsolute ? uri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative : m_UriKind});
  92.             }
  93.            
  94.             if (uri != null && destinationType == typeof(string))
  95.                 return uri.OriginalString;
  96.            
  97.             if (uri != null && destinationType == typeof(Uri))
  98.                 return new Uri(uri.OriginalString, m_UriKind == UriKind.RelativeOrAbsolute ? uri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative : m_UriKind);
  99.            
  100.             return base.ConvertTo(context, culture, value, destinationType);
  101.         }
  102.         //
  103.         public override bool IsValid(ITypeDescriptorContext context, object value)
  104.         {
  105.             string str = value as string;
  106.             Uri temp;
  107.             if (str != null)
  108.                 return Uri.TryCreate(str, m_UriKind, out temp);
  109.            
  110.             return value is Uri;
  111.         }
  112.     }
  113.    
  114. }

Developer Fusion