The Labs \ Source Viewer \ SSCLI \ System.Xml.Serialization.Configuration \ TypeTypeConverter

  1. //------------------------------------------------------------------------------
  2. // <copyright file="SchemaImporterExtensionElement.cs" company="Microsoft Corporation">
  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. // <owner current="true" primary="true">ElenaK</owner>
  15. //------------------------------------------------------------------------------
  16. namespace System.Xml.Serialization.Configuration
  17. {
  18.     using System;
  19.     using System.Configuration;
  20.     using System.ComponentModel;
  21.     using System.Globalization;
  22.     using System.Reflection;
  23.     using System.Security.Permissions;
  24.    
  25.     public sealed class SchemaImporterExtensionElement : ConfigurationElement
  26.     {
  27.         public SchemaImporterExtensionElement()
  28.         {
  29.             this.properties.Add(this.name);
  30.             this.properties.Add(this.type);
  31.         }
  32.        
  33.         public SchemaImporterExtensionElement(string name, string type) : this()
  34.         {
  35.             this.Name = name;
  36.             this[this.type] = new TypeAndName(type);
  37.         }
  38.        
  39.         public SchemaImporterExtensionElement(string name, Type type) : this()
  40.         {
  41.             this.Name = name;
  42.             this.Type = type;
  43.         }
  44.        
  45.         [ConfigurationProperty(ConfigurationStrings.Name, IsRequired = true, IsKey = true)]
  46.         public string Name {
  47.             get { return (string)this[this.name]; }
  48.             set { this[this.name] = value; }
  49.         }
  50.        
  51.         protected override ConfigurationPropertyCollection Properties {
  52.             get { return this.properties; }
  53.         }
  54.        
  55.         [ConfigurationProperty(ConfigurationStrings.Type, IsRequired = true, IsKey = false)]
  56.         [TypeConverter(typeof(TypeTypeConverter))]
  57.         public Type Type {
  58.             get { return ((TypeAndName)this[this.type]).type; }
  59.             set { this[this.type] = new TypeAndName(value); }
  60.         }
  61.        
  62.         internal string Key {
  63.             get { return this.Name; }
  64.         }
  65.        
  66.         ConfigurationPropertyCollection properties = new ConfigurationPropertyCollection();
  67.        
  68.         readonly ConfigurationProperty name = new ConfigurationProperty(ConfigurationStrings.Name, typeof(string), null, ConfigurationPropertyOptions.IsKey);
  69.        
  70.         readonly ConfigurationProperty type = new ConfigurationProperty(ConfigurationStrings.Type, typeof(Type), null, new TypeTypeConverter(), null, ConfigurationPropertyOptions.IsRequired);
  71.        
  72.         class TypeAndName
  73.         {
  74.             public TypeAndName(string name)
  75.             {
  76.                 this.type = Type.GetType(name, true, true);
  77.                 this.name = name;
  78.             }
  79.            
  80.             public TypeAndName(Type type)
  81.             {
  82.                 this.type = type;
  83.             }
  84.            
  85.             public override int GetHashCode()
  86.             {
  87.                 return type.GetHashCode();
  88.             }
  89.            
  90.             public override bool Equals(object comparand)
  91.             {
  92.                 return type.Equals(((TypeAndName)comparand).type);
  93.             }
  94.            
  95.             public readonly Type type;
  96.             public readonly string name;
  97.         }
  98.        
  99.         class TypeTypeConverter : TypeConverter
  100.         {
  101.             public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  102.             {
  103.                 if (sourceType == typeof(string)) {
  104.                     return true;
  105.                 }
  106.                 return base.CanConvertFrom(context, sourceType);
  107.             }
  108.            
  109.             public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  110.             {
  111.                 if (value is string) {
  112.                     return new TypeAndName((string)value);
  113.                 }
  114.                
  115.                 return base.ConvertFrom(context, culture, value);
  116.             }
  117.            
  118.             public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  119.             {
  120.                 if (destinationType == typeof(string)) {
  121.                     TypeAndName castedValue = (TypeAndName)value;
  122.                     return castedValue.name == null ? castedValue.type.AssemblyQualifiedName : castedValue.name;
  123.                 }
  124.                
  125.                 return base.ConvertTo(context, culture, value, destinationType);
  126.             }
  127.         }
  128.     }
  129. }

Developer Fusion