The Labs \ Source Viewer \ SSCLI \ System.ComponentModel \ GuidConverter

  1. //------------------------------------------------------------------------------
  2. // <copyright file="GuidConverter.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. */
  17. namespace System.ComponentModel
  18. {
  19.     using Microsoft.Win32;
  20.     using System.ComponentModel.Design.Serialization;
  21.     using System.Diagnostics;
  22.     using System.Globalization;
  23.     using System.Reflection;
  24.     using System.Runtime.InteropServices;
  25.     using System.Runtime.Remoting;
  26.     using System.Runtime.Serialization.Formatters;
  27.     using System.Security.Permissions;
  28.    
  29.     /// <devdoc>
  30.     /// <para>Provides a
  31.     /// type converter to convert globally unique identifier objects to and from various
  32.     /// other representations.</para>
  33.     /// </devdoc>
  34.     [HostProtection(SharedState = true)]
  35.     public class GuidConverter : TypeConverter
  36.     {
  37.        
  38.         /// <devdoc>
  39.         /// <para>Gets a value indicating whether this
  40.         /// converter can convert an object in the given source type to a globally unique identifier object
  41.         /// using the context.</para>
  42.         /// </devdoc>
  43.         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  44.         {
  45.             if (sourceType == typeof(string)) {
  46.                 return true;
  47.             }
  48.             return base.CanConvertFrom(context, sourceType);
  49.         }
  50.        
  51.         /// <devdoc>
  52.         /// <para>Gets a value indicating whether this converter can
  53.         /// convert an object to the given destination type using the context.</para>
  54.         /// </devdoc>
  55.         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  56.         {
  57.             if (destinationType == typeof(InstanceDescriptor)) {
  58.                 return true;
  59.             }
  60.             return base.CanConvertTo(context, destinationType);
  61.         }
  62.        
  63.         /// <devdoc>
  64.         /// <para>Converts
  65.         /// the given object to a globally unique identifier object.</para>
  66.         /// </devdoc>
  67.         public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  68.         {
  69.             if (value is string) {
  70.                 string text = ((string)value).Trim();
  71.                 return new Guid(text);
  72.             }
  73.             return base.ConvertFrom(context, culture, value);
  74.         }
  75.        
  76.         /// <devdoc>
  77.         /// Converts the given object to another type. The most common types to convert
  78.         /// are to and from a string object. The default implementation will make a call
  79.         /// to ToString on the object if the object is valid and if the destination
  80.         /// type is string. If this cannot convert to the desitnation type, this will
  81.         /// throw a NotSupportedException.
  82.         /// </devdoc>
  83.         public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  84.         {
  85.             if (destinationType == null) {
  86.                 throw new ArgumentNullException("destinationType");
  87.             }
  88.            
  89.             if (destinationType == typeof(InstanceDescriptor) && value is Guid) {
  90.                 ConstructorInfo ctor = typeof(Guid).GetConstructor(new Type[] {typeof(string)});
  91.                 if (ctor != null) {
  92.                     return new InstanceDescriptor(ctor, new object[] {value.ToString()});
  93.                 }
  94.             }
  95.            
  96.             return base.ConvertTo(context, culture, value, destinationType);
  97.         }
  98.     }
  99. }

Developer Fusion