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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="TypeListConverter.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.Collections;
  21.     using System.ComponentModel.Design.Serialization;
  22.     using System.Diagnostics;
  23.     using System.Globalization;
  24.     using System.Reflection;
  25.     using System.Runtime.InteropServices;
  26.     using System.Runtime.Remoting;
  27.     using System.Runtime.Serialization.Formatters;
  28.     using System.Security.Permissions;
  29.    
  30.     /// <devdoc>
  31.     /// <para>Provides a type
  32.     /// converter that can be used to populate a list box with available types.</para>
  33.     /// </devdoc>
  34.     [HostProtection(SharedState = true)]
  35.     public abstract class TypeListConverter : TypeConverter
  36.     {
  37.         private Type[] types;
  38.         private StandardValuesCollection values;
  39.        
  40.         /// <devdoc>
  41.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.TypeListConverter'/> class using
  42.         /// the type array as the available types.</para>
  43.         /// </devdoc>
  44.         protected TypeListConverter(Type[] types)
  45.         {
  46.             this.types = types;
  47.         }
  48.        
  49.         /// <internalonly/>
  50.         /// <devdoc>
  51.         /// <para>Gets a value indicating whether this converter
  52.         /// can convert an object in the given source type to an enumeration object using
  53.         /// the specified context.</para>
  54.         /// </devdoc>
  55.         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  56.         {
  57.             if (sourceType == typeof(string)) {
  58.                 return true;
  59.             }
  60.             return base.CanConvertFrom(context, sourceType);
  61.         }
  62.        
  63.         /// <devdoc>
  64.         /// <para>Gets a value indicating whether this converter can
  65.         /// convert an object to the given destination type using the context.</para>
  66.         /// </devdoc>
  67.         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  68.         {
  69.             if (destinationType == typeof(InstanceDescriptor)) {
  70.                 return true;
  71.             }
  72.             return base.CanConvertTo(context, destinationType);
  73.         }
  74.        
  75.         /// <internalonly/>
  76.         /// <devdoc>
  77.         /// <para>Converts the specified value object to an enumeration object.</para>
  78.         /// </devdoc>
  79.         public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  80.         {
  81.             if (value is string) {
  82.                 foreach (Type t in types) {
  83.                     if (value.Equals(t.FullName)) {
  84.                         return t;
  85.                     }
  86.                 }
  87.             }
  88.            
  89.             return base.ConvertFrom(context, culture, value);
  90.         }
  91.        
  92.         /// <internalonly/>
  93.         /// <devdoc>
  94.         /// <para>Converts the given value object to the specified destination type.</para>
  95.         /// </devdoc>
  96.         public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  97.         {
  98.             if (destinationType == null) {
  99.                 throw new ArgumentNullException("destinationType");
  100.             }
  101.            
  102.             if (destinationType == typeof(string)) {
  103.                 if (value == null) {
  104.                     return SR.GetString(SR.toStringNone);
  105.                 }
  106.                 else {
  107.                     return ((Type)value).FullName;
  108.                 }
  109.             }
  110.             if (destinationType == typeof(InstanceDescriptor) && value is Type) {
  111.                 MethodInfo method = typeof(Type).GetMethod("GetType", new Type[] {typeof(string)});
  112.                 if (method != null) {
  113.                     return new InstanceDescriptor(method, new object[] {((Type)value).AssemblyQualifiedName});
  114.                 }
  115.             }
  116.            
  117.             return base.ConvertTo(context, culture, value, destinationType);
  118.         }
  119.        
  120.         /// <internalonly/>
  121.         /// <devdoc>
  122.         /// <para>Gets a collection of standard values for the data type this validator is
  123.         /// designed for.</para>
  124.         /// </devdoc>
  125.         public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  126.         {
  127.             if (values == null) {
  128.                 object[] objTypes;
  129.                
  130.                 if (types != null) {
  131.                     objTypes = new object[types.Length];
  132.                     Array.Copy(types, objTypes, types.Length);
  133.                 }
  134.                 else {
  135.                     objTypes = null;
  136.                 }
  137.                
  138.                 values = new StandardValuesCollection(objTypes);
  139.             }
  140.             return values;
  141.         }
  142.        
  143.         /// <internalonly/>
  144.         /// <devdoc>
  145.         /// <para>Gets a value indicating whether the list of standard values returned from
  146.         /// <see cref='System.ComponentModel.TypeListConverter.GetStandardValues'/> is an exclusive list. </para>
  147.         /// </devdoc>
  148.         public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
  149.         {
  150.             return true;
  151.         }
  152.        
  153.         /// <internalonly/>
  154.         /// <devdoc>
  155.         /// <para>Gets a value indicating whether this object supports a
  156.         /// standard set of values that can be picked from a list using the specified
  157.         /// context.</para>
  158.         /// </devdoc>
  159.         public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  160.         {
  161.             return true;
  162.         }
  163.     }
  164. }

Developer Fusion