-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicConverter.cs
160 lines (142 loc) · 6.98 KB
/
DynamicConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using WebServiceStudio.Dialogs;
namespace WebServiceStudio
{
internal class DynamicConverter : TypeConverter
{
private static Hashtable converterTable = null;
private static string typeNotFoundMessage = "ProxyPropertiesType {0} specified in WebServiceStudio.exe.options is not found";
static DynamicConverter()
{
converterTable = new Hashtable();
converterTable[typeof(bool)] = Activator.CreateInstance(typeof(BooleanConverter));
converterTable[typeof(byte)] = Activator.CreateInstance(typeof(ByteConverter));
converterTable[typeof(sbyte)] = Activator.CreateInstance(typeof(SByteConverter));
converterTable[typeof(char)] = Activator.CreateInstance(typeof(CharConverter));
converterTable[typeof(double)] = Activator.CreateInstance(typeof(DoubleConverter));
converterTable[typeof(string)] = Activator.CreateInstance(typeof(StringConverter));
converterTable[typeof(int)] = Activator.CreateInstance(typeof(Int32Converter));
converterTable[typeof(short)] = Activator.CreateInstance(typeof(Int16Converter));
converterTable[typeof(long)] = Activator.CreateInstance(typeof(Int64Converter));
converterTable[typeof(float)] = Activator.CreateInstance(typeof(SingleConverter));
converterTable[typeof(ushort)] = Activator.CreateInstance(typeof(UInt16Converter));
converterTable[typeof(uint)] = Activator.CreateInstance(typeof(UInt32Converter));
converterTable[typeof(ulong)] = Activator.CreateInstance(typeof(UInt64Converter));
converterTable[typeof(object)] = Activator.CreateInstance(typeof(ExpandableObjectConverter));
converterTable[typeof(void)] = Activator.CreateInstance(typeof(TypeConverter));
converterTable[typeof(CultureInfo)] = Activator.CreateInstance(typeof(CultureInfoConverter));
converterTable[typeof(DateTime)] = Activator.CreateInstance(typeof(DateTimeConverter));
converterTable[typeof(decimal)] = Activator.CreateInstance(typeof(DecimalConverter));
converterTable[typeof(TimeSpan)] = Activator.CreateInstance(typeof(TimeSpanConverter));
converterTable[typeof(Guid)] = Activator.CreateInstance(typeof(GuidConverter));
foreach (CustomHandler handler in Configuration.MasterConfig.TypeConverters)
{
Type type = Type.GetType(handler.TypeName);
if (type == null)
{
NewMainForm.ShowMessage(typeof(DynamicConverter), MessageType.Warning, string.Format(typeNotFoundMessage, handler.TypeName));
}
else
{
Type type2 = Type.GetType(handler.Handler);
if (type2 == null)
{
NewMainForm.ShowMessage(typeof(DynamicConverter), MessageType.Warning, string.Format(typeNotFoundMessage, handler.Handler));
}
else
{
converterTable[type] = Activator.CreateInstance(type2);
}
}
}
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return GetConverter(context).CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return GetConverter(context).CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
return GetConverter(context).ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
return GetConverter(context).ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
return GetConverter(context).CreateInstance(context, propertyValues);
}
private static Type GetContainedType(ITypeDescriptorContext context)
{
if (context != null)
{
TreeNodeProperty instance = context.Instance as TreeNodeProperty;
if (instance != null)
{
return instance.Type;
}
}
return null;
}
private static TypeConverter GetConverter(ITypeDescriptorContext context)
{
object obj2 = null;
Type containedType = GetContainedType(context);
if (containedType != null)
{
obj2 = converterTable[containedType];
}
if (obj2 == null)
{
if ((containedType != null) && containedType.IsEnum)
{
obj2 = converterTable[containedType] = new EnumConverter(containedType);
}
else
{
obj2 = converterTable[typeof(object)];
}
}
return (obj2 as TypeConverter);
}
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return GetConverter(context).GetCreateInstanceSupported(context);
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
return GetConverter(context).GetProperties(context, value, attributes);
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return GetConverter(context).GetPropertiesSupported(context);
}
public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return GetConverter(context).GetStandardValues(context);
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return GetConverter(context).GetStandardValuesExclusive(context);
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return GetConverter(context).GetStandardValuesSupported(context);
}
public static bool IsConverterDefined(Type type)
{
return (converterTable[type] != null);
}
public override bool IsValid(ITypeDescriptorContext context, object value)
{
return GetConverter(context).IsValid(context, value);
}
}
}