Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / System.Xml / System / Xml / Serialization / Configuration / SchemaImporterExtensionElement.cs
blob8a5c5b1dd1b57c3370538d6f04bc98decf99d873
1 //------------------------------------------------------------------------------
2 // <copyright file="SchemaImporterExtensionElement.cs" company="Microsoft Corporation">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
7 #if CONFIGURATION_DEP
8 namespace System.Xml.Serialization.Configuration
10 using System;
11 using System.Configuration;
12 using System.ComponentModel;
13 using System.Globalization;
14 using System.Reflection;
15 using System.Security.Permissions;
17 public sealed class SchemaImporterExtensionElement : ConfigurationElement
19 public SchemaImporterExtensionElement()
21 this.properties.Add(this.name);
22 this.properties.Add(this.type);
25 public SchemaImporterExtensionElement(string name, string type) : this()
27 this.Name = name;
28 this[this.type] = new TypeAndName(type);
31 public SchemaImporterExtensionElement(string name, Type type) : this()
33 this.Name = name;
34 this.Type = type;
37 [ConfigurationProperty(ConfigurationStrings.Name, IsRequired=true, IsKey = true)]
38 public string Name
40 get { return (string)this[this.name]; }
41 set { this[this.name] = value; }
44 protected override ConfigurationPropertyCollection Properties
46 get
48 return this.properties;
52 [ConfigurationProperty(ConfigurationStrings.Type, IsRequired=true, IsKey = false)]
53 [TypeConverter(typeof(TypeTypeConverter))]
54 public Type Type
56 get { return ((TypeAndName) this[this.type]).type; }
57 set { this[this.type] = new TypeAndName(value); }
60 internal string Key
62 get { return this.Name; }
65 ConfigurationPropertyCollection properties = new ConfigurationPropertyCollection();
67 readonly ConfigurationProperty name =
68 new ConfigurationProperty(ConfigurationStrings.Name, typeof(string), null,
69 ConfigurationPropertyOptions.IsKey);
71 readonly ConfigurationProperty type =
72 new ConfigurationProperty(ConfigurationStrings.Type, typeof(Type), null,
73 new TypeTypeConverter(), null, ConfigurationPropertyOptions.IsRequired);
75 class TypeAndName
77 public TypeAndName(string name)
79 this.type = Type.GetType(name, true, true);
80 this.name = name;
83 public TypeAndName(Type type)
85 this.type = type;
88 public override int GetHashCode()
90 return type.GetHashCode();
93 public override bool Equals(object comparand)
95 return type.Equals(((TypeAndName) comparand).type);
98 public readonly Type type;
99 public readonly string name;
102 class TypeTypeConverter : TypeConverter {
103 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
104 if (sourceType == typeof(string)) {
105 return true;
107 return base.CanConvertFrom(context, sourceType);
110 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
111 if (value is string) {
112 return new TypeAndName((string) value);
115 return base.ConvertFrom(context, culture, value);
118 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
119 if (destinationType == typeof(string)) {
120 TypeAndName castedValue = (TypeAndName) value;
121 return castedValue.name == null ? castedValue.type.AssemblyQualifiedName : castedValue.name;
124 return base.ConvertTo(context, culture, value, destinationType);
129 #endif