1
|
|
|
2
|
|
#region Apache Notice
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
#endregion
|
24
|
|
|
25
|
|
#region Remarks
|
26
|
|
|
27
|
|
#endregion
|
28
|
|
|
29
|
|
#region Imports
|
30
|
|
|
31
|
|
using System;
|
32
|
|
using System.Reflection;
|
33
|
|
|
34
|
|
using IBatisNet.Common.Exceptions;
|
35
|
|
#endregion
|
36
|
|
|
37
|
|
namespace IBatisNet.Common.Utilities.TypesResolver
|
38
|
|
{
|
39
|
|
|
40
|
|
|
41
|
|
|
42
|
|
|
43
|
|
|
44
|
|
|
45
|
|
|
46
|
|
|
47
|
|
|
48
|
|
|
49
|
|
|
50
|
|
public class TypeResolver
|
51
|
|
{
|
52
|
|
#region Constructor (s) / Destructor
|
53
|
|
|
54
|
|
|
55
|
|
|
56
|
1361
|
public TypeResolver () {}
|
57
|
|
#endregion
|
58
|
|
|
59
|
|
#region Methods
|
60
|
|
|
61
|
|
|
62
|
|
|
63
|
|
|
64
|
|
|
65
|
|
|
66
|
|
|
67
|
|
|
68
|
|
|
69
|
|
|
70
|
|
|
71
|
|
|
72
|
|
|
73
|
37
|
public virtual Type Resolve (string typeName)
|
74
|
|
{
|
75
|
|
|
76
|
|
#region Sanity Check
|
77
|
37
|
if (typeName == null || typeName.Trim().Length==0)
|
78
|
|
{
|
79
|
0
|
throw new ConfigurationException (
|
80
|
|
"Could not load type with a null or zero length parameter.");
|
81
|
|
}
|
82
|
|
#endregion
|
83
|
|
|
84
|
37
|
Type type = null;
|
85
|
37
|
string canonicalTypeName = TypeAliasResolver.Resolve (typeName);
|
86
|
37
|
TypeAssemblyInfo typeInfo = new TypeAssemblyInfo (canonicalTypeName);
|
87
|
37
|
if (typeInfo.IsAssemblyQualified)
|
88
|
|
{
|
89
|
|
|
90
|
16
|
Assembly assembly = Assembly.LoadWithPartialName (typeInfo.AssemblyName);
|
91
|
16
|
if (assembly != null)
|
92
|
|
{
|
93
|
16
|
type = assembly.GetType (typeInfo.TypeName, true, true);
|
94
|
|
}
|
95
|
|
}
|
96
|
|
else
|
97
|
|
{
|
98
|
|
|
99
|
21
|
Assembly [] assemblies = AppDomain.CurrentDomain.GetAssemblies ();
|
100
|
21
|
foreach (Assembly assembly in assemblies)
|
101
|
|
{
|
102
|
21
|
type = assembly.GetType (typeInfo.TypeName, false, false);
|
103
|
21
|
if (type != null)
|
104
|
|
{
|
105
|
21
|
break;
|
106
|
|
}
|
107
|
|
}
|
108
|
|
}
|
109
|
37
|
if (type == null)
|
110
|
|
{
|
111
|
0
|
throw new TypeLoadException (
|
112
|
|
"Could not load type : " + typeName);
|
113
|
|
}
|
114
|
37
|
return type;
|
115
|
|
}
|
116
|
|
#endregion
|
117
|
|
|
118
|
|
#region Inner Class : TypeAssemblyInfo
|
119
|
|
|
120
|
|
|
121
|
|
|
122
|
|
|
123
|
|
internal class TypeAssemblyInfo
|
124
|
|
{
|
125
|
|
#region Constants
|
126
|
|
|
127
|
|
|
128
|
|
|
129
|
|
|
130
|
|
|
131
|
|
public const string TypeAssemblySeparator = ",";
|
132
|
|
#endregion
|
133
|
|
|
134
|
|
#region Fields
|
135
|
|
private string unresolvedAssemblyName;
|
136
|
|
private string unresolvedTypeName;
|
137
|
|
#endregion
|
138
|
|
|
139
|
|
#region Properties
|
140
|
|
|
141
|
|
|
142
|
|
|
143
|
|
public string TypeName
|
144
|
|
{
|
145
|
37
|
get
|
146
|
|
{
|
147
|
37
|
return unresolvedTypeName;
|
148
|
|
}
|
149
|
|
}
|
150
|
|
|
151
|
|
|
152
|
|
|
153
|
|
|
154
|
|
public string AssemblyName
|
155
|
|
{
|
156
|
69
|
get
|
157
|
|
{
|
158
|
69
|
return unresolvedAssemblyName;
|
159
|
|
}
|
160
|
|
}
|
161
|
|
|
162
|
|
|
163
|
|
|
164
|
|
|
165
|
|
public bool IsAssemblyQualified
|
166
|
|
{
|
167
|
37
|
get
|
168
|
|
{
|
169
|
37
|
if (AssemblyName == null || AssemblyName.Trim().Length==0)
|
170
|
|
{
|
171
|
21
|
return false;
|
172
|
|
}
|
173
|
|
else
|
174
|
|
{
|
175
|
16
|
return true;
|
176
|
|
}
|
177
|
|
}
|
178
|
|
}
|
179
|
|
|
180
|
|
|
181
|
|
|
182
|
|
|
183
|
0
|
public string OriginalTypeName
|
184
|
|
{
|
185
|
|
get
|
186
|
|
{
|
187
|
|
System.Text.StringBuilder buffer
|
188
|
|
= new System.Text.StringBuilder (TypeName);
|
189
|
|
if (IsAssemblyQualified)
|
190
|
|
{
|
191
|
|
buffer.Append (TypeAssemblySeparator);
|
192
|
|
buffer.Append (AssemblyName);
|
193
|
|
}
|
194
|
|
return buffer.ToString ();
|
195
|
|
}
|
196
|
|
}
|
197
|
|
#endregion
|
198
|
|
|
199
|
|
#region Constructor (s) / Destructor
|
200
|
|
|
201
|
|
|
202
|
|
|
203
|
|
|
204
|
|
|
205
|
|
|
206
|
37
|
public TypeAssemblyInfo (string unresolvedTypeName)
|
207
|
|
{
|
208
|
37
|
SplitTypeAndAssemblyNames (unresolvedTypeName);
|
209
|
|
}
|
210
|
|
#endregion
|
211
|
|
|
212
|
|
|
213
|
|
#region Methods
|
214
|
37
|
private void SplitTypeAndAssemblyNames (string originalTypeName)
|
215
|
|
{
|
216
|
37
|
int typeAssemblyIndex
|
217
|
|
= originalTypeName.IndexOf (
|
218
|
|
TypeAssemblyInfo.TypeAssemblySeparator);
|
219
|
37
|
if (typeAssemblyIndex < 0)
|
220
|
|
{
|
221
|
21
|
unresolvedTypeName = originalTypeName;
|
222
|
|
}
|
223
|
|
else
|
224
|
|
{
|
225
|
16
|
unresolvedTypeName = originalTypeName.Substring (
|
226
|
|
0, typeAssemblyIndex).Trim ();
|
227
|
16
|
unresolvedAssemblyName = originalTypeName.Substring (
|
228
|
|
typeAssemblyIndex + 1).Trim ();
|
229
|
|
}
|
230
|
|
}
|
231
|
|
#endregion
|
232
|
|
|
233
|
|
}
|
234
|
|
#endregion
|
235
|
|
}
|
236
|
|
}
|
237
|
|
|