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