Terraria
v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
TypeNameBuilder.cs
Go to the documentation of this file.
1
using
System.Collections.Generic
;
2
using
System.Text
;
3
4
namespace
System.Reflection.Emit
;
5
6
internal
sealed
class
TypeNameBuilder
7
{
8
internal
enum
Format
9
{
10
ToString
,
11
FullName
,
12
AssemblyQualifiedName
13
}
14
15
private
StringBuilder
_str
=
new
StringBuilder
();
16
17
private
int
_instNesting
;
18
19
private
bool
_firstInstArg
;
20
21
private
bool
_nestedName
;
22
23
private
bool
_hasAssemblySpec
;
24
25
private
bool
_useAngleBracketsForGenerics
;
26
27
private
List<int>
_stack
=
new
List<int>
();
28
29
private
int
_stackIdx
;
30
31
private
TypeNameBuilder
()
32
{
33
}
34
35
private
void
OpenGenericArguments
()
36
{
37
_instNesting
++;
38
_firstInstArg
=
true
;
39
if
(
_useAngleBracketsForGenerics
)
40
{
41
Append
(
'<'
);
42
}
43
else
44
{
45
Append
(
'['
);
46
}
47
}
48
49
private
void
CloseGenericArguments
()
50
{
51
_instNesting
--;
52
if
(
_firstInstArg
)
53
{
54
_str
.
Remove
(
_str
.
Length
- 1, 1);
55
}
56
else
if
(
_useAngleBracketsForGenerics
)
57
{
58
Append
(
'>'
);
59
}
60
else
61
{
62
Append
(
']'
);
63
}
64
}
65
66
private
void
OpenGenericArgument
()
67
{
68
_nestedName
=
false
;
69
if
(!
_firstInstArg
)
70
{
71
Append
(
','
);
72
}
73
_firstInstArg
=
false
;
74
if
(
_useAngleBracketsForGenerics
)
75
{
76
Append
(
'<'
);
77
}
78
else
79
{
80
Append
(
'['
);
81
}
82
PushOpenGenericArgument
();
83
}
84
85
private
void
CloseGenericArgument
()
86
{
87
if
(
_hasAssemblySpec
)
88
{
89
if
(
_useAngleBracketsForGenerics
)
90
{
91
Append
(
'>'
);
92
}
93
else
94
{
95
Append
(
']'
);
96
}
97
}
98
PopOpenGenericArgument
();
99
}
100
101
private
void
AddName
(
string
name)
102
{
103
if
(
_nestedName
)
104
{
105
Append
(
'+'
);
106
}
107
_nestedName
=
true
;
108
EscapeName
(name);
109
}
110
111
private
void
AddArray
(
int
rank
)
112
{
113
if
(
rank
== 1)
114
{
115
Append
(
"[*]"
);
116
return
;
117
}
118
if
(
rank
> 64)
119
{
120
_str
.
Append
(
'['
).
Append
(
rank
).
Append
(
']'
);
121
return
;
122
}
123
Append
(
'['
);
124
for
(
int
i = 1; i <
rank
; i++)
125
{
126
Append
(
','
);
127
}
128
Append
(
']'
);
129
}
130
131
private
void
AddAssemblySpec
(
string
assemblySpec
)
132
{
133
if
(
assemblySpec
!=
null
&& !
assemblySpec
.Equals(
""
))
134
{
135
Append
(
", "
);
136
if
(
_instNesting
> 0)
137
{
138
EscapeEmbeddedAssemblyName
(
assemblySpec
);
139
}
140
else
141
{
142
EscapeAssemblyName
(
assemblySpec
);
143
}
144
_hasAssemblySpec
=
true
;
145
}
146
}
147
148
public
override
string
ToString
()
149
{
150
return
_str
.
ToString
();
151
}
152
153
private
static
bool
ContainsReservedChar
(
string
name)
154
{
155
foreach
(
char
c
in
name)
156
{
157
if
(c ==
'\0'
)
158
{
159
break
;
160
}
161
if
(
IsTypeNameReservedChar
(c))
162
{
163
return
true
;
164
}
165
}
166
return
false
;
167
}
168
169
private
static
bool
IsTypeNameReservedChar
(
char
ch
)
170
{
171
switch
(
ch
)
172
{
173
case
'&'
:
174
case
'*'
:
175
case
'+'
:
176
case
','
:
177
case
'['
:
178
case
'\\'
:
179
case
']'
:
180
return
true
;
181
default
:
182
return
false
;
183
}
184
}
185
186
private
void
EscapeName
(
string
name)
187
{
188
if
(
ContainsReservedChar
(name))
189
{
190
foreach
(
char
c
in
name)
191
{
192
if
(c != 0)
193
{
194
if
(
IsTypeNameReservedChar
(c))
195
{
196
_str
.
Append
(
'\\'
);
197
}
198
_str
.
Append
(c);
199
continue
;
200
}
201
break
;
202
}
203
}
204
else
205
{
206
Append
(name);
207
}
208
}
209
210
private
void
EscapeAssemblyName
(
string
name)
211
{
212
Append
(name);
213
}
214
215
private
void
EscapeEmbeddedAssemblyName
(
string
name)
216
{
217
if
(name.Contains(
']'
))
218
{
219
foreach
(
char
c
in
name)
220
{
221
if
(c ==
']'
)
222
{
223
Append
(
'\\'
);
224
}
225
Append
(c);
226
}
227
}
228
else
229
{
230
Append
(name);
231
}
232
}
233
234
private
void
PushOpenGenericArgument
()
235
{
236
_stack
.
Add
(
_str
.
Length
);
237
_stackIdx
++;
238
}
239
240
private
void
PopOpenGenericArgument
()
241
{
242
int
num =
_stack
[--
_stackIdx
];
243
_stack
.
RemoveAt
(
_stackIdx
);
244
if
(!
_hasAssemblySpec
)
245
{
246
_str
.
Remove
(num - 1, 1);
247
}
248
_hasAssemblySpec
=
false
;
249
}
250
251
private
void
Append
(
string
pStr
)
252
{
253
foreach
(
char
c
in
pStr
)
254
{
255
if
(c != 0)
256
{
257
_str
.
Append
(c);
258
continue
;
259
}
260
break
;
261
}
262
}
263
264
private
void
Append
(
char
c)
265
{
266
_str
.
Append
(c);
267
}
268
269
internal
static
string
ToString
(
Type
type
,
Format
format
)
270
{
271
if
((
format
==
Format
.FullName ||
format
==
Format
.AssemblyQualifiedName) && !
type
.IsGenericTypeDefinition &&
type
.ContainsGenericParameters)
272
{
273
return
null
;
274
}
275
TypeNameBuilder
typeNameBuilder
=
new
TypeNameBuilder
();
276
typeNameBuilder
.AddAssemblyQualifiedName(
type
,
format
);
277
return
typeNameBuilder
.ToString();
278
}
279
280
private
void
AddElementType
(
Type
type
)
281
{
282
if
(
type
.HasElementType)
283
{
284
AddElementType
(
type
.GetElementType());
285
if
(
type
.IsPointer)
286
{
287
Append
(
'*'
);
288
}
289
else
if
(
type
.IsByRef)
290
{
291
Append
(
'&'
);
292
}
293
else
if
(
type
.IsSZArray)
294
{
295
Append
(
"[]"
);
296
}
297
else
if
(
type
.IsArray)
298
{
299
AddArray
(
type
.GetArrayRank());
300
}
301
}
302
}
303
304
private
void
AddAssemblyQualifiedName
(
Type
type
,
Format
format
)
305
{
306
Type
type2
=
type
;
307
while
(
type2
.HasElementType)
308
{
309
type2
=
type2
.GetElementType();
310
}
311
List<Type>
list
=
new
List<Type>
();
312
Type
type3
=
type2
;
313
while
(
type3
!=
null
)
314
{
315
list
.
Add
(
type3
);
316
type3
= (type3.IsGenericParameter ?
null
:
type3
.DeclaringType);
317
}
318
for
(
int
num =
list
.Count - 1; num >= 0; num--)
319
{
320
Type
type4
=
list
[num];
321
string
text
=
type4
.Name;
322
if
(num ==
list
.Count - 1 &&
type4
.Namespace !=
null
&&
type4
.Namespace.Length != 0)
323
{
324
text
= type4.Namespace +
"."
+
text
;
325
}
326
AddName
(
text
);
327
}
328
if
(
type2
.IsGenericType && (!
type2
.IsGenericTypeDefinition ||
format
==
Format
.ToString))
329
{
330
Type
[]
genericArguments
=
type2
.GetGenericArguments();
331
OpenGenericArguments
();
332
for
(
int
i = 0; i <
genericArguments
.Length; i++)
333
{
334
Format
format2
= ((
format
==
Format
.FullName) ?
Format
.AssemblyQualifiedName :
format
);
335
OpenGenericArgument
();
336
AddAssemblyQualifiedName
(
genericArguments
[i],
format2
);
337
CloseGenericArgument
();
338
}
339
CloseGenericArguments
();
340
}
341
AddElementType
(
type
);
342
if
(
format
==
Format
.AssemblyQualifiedName)
343
{
344
AddAssemblySpec
(
type
.Module.Assembly.FullName);
345
}
346
}
347
}
System.Collections.Generic.Dictionary.Add
void Add(TKey key, TValue value)
Definition
Dictionary.cs:873
System.Collections.Generic.Dictionary
Definition
Dictionary.cs:14
System.Collections.Generic.List.RemoveAt
void RemoveAt(int index)
Definition
List.cs:824
System.Collections.Generic.List.Add
void Add(T item)
Definition
List.cs:236
System.Reflection.Emit.TypeNameBuilder.PopOpenGenericArgument
void PopOpenGenericArgument()
Definition
TypeNameBuilder.cs:240
System.Reflection.Emit.TypeNameBuilder.TypeNameBuilder
TypeNameBuilder()
Definition
TypeNameBuilder.cs:31
System.Reflection.Emit.TypeNameBuilder.EscapeAssemblyName
void EscapeAssemblyName(string name)
Definition
TypeNameBuilder.cs:210
System.Reflection.Emit.TypeNameBuilder._stack
List< int > _stack
Definition
TypeNameBuilder.cs:27
System.Reflection.Emit.TypeNameBuilder._instNesting
int _instNesting
Definition
TypeNameBuilder.cs:17
System.Reflection.Emit.TypeNameBuilder.AddName
void AddName(string name)
Definition
TypeNameBuilder.cs:101
System.Reflection.Emit.TypeNameBuilder._hasAssemblySpec
bool _hasAssemblySpec
Definition
TypeNameBuilder.cs:23
System.Reflection.Emit.TypeNameBuilder.Append
void Append(string pStr)
Definition
TypeNameBuilder.cs:251
System.Reflection.Emit.TypeNameBuilder.Format
Format
Definition
TypeNameBuilder.cs:9
System.Reflection.Emit.TypeNameBuilder.Format.AssemblyQualifiedName
@ AssemblyQualifiedName
System.Reflection.Emit.TypeNameBuilder.Format.FullName
@ FullName
System.Reflection.Emit.TypeNameBuilder.OpenGenericArguments
void OpenGenericArguments()
Definition
TypeNameBuilder.cs:35
System.Reflection.Emit.TypeNameBuilder.IsTypeNameReservedChar
static bool IsTypeNameReservedChar(char ch)
Definition
TypeNameBuilder.cs:169
System.Reflection.Emit.TypeNameBuilder.EscapeEmbeddedAssemblyName
void EscapeEmbeddedAssemblyName(string name)
Definition
TypeNameBuilder.cs:215
System.Reflection.Emit.TypeNameBuilder.EscapeName
void EscapeName(string name)
Definition
TypeNameBuilder.cs:186
System.Reflection.Emit.TypeNameBuilder.ToString
override string ToString()
Definition
TypeNameBuilder.cs:148
System.Reflection.Emit.TypeNameBuilder._firstInstArg
bool _firstInstArg
Definition
TypeNameBuilder.cs:19
System.Reflection.Emit.TypeNameBuilder.CloseGenericArguments
void CloseGenericArguments()
Definition
TypeNameBuilder.cs:49
System.Reflection.Emit.TypeNameBuilder.AddAssemblyQualifiedName
void AddAssemblyQualifiedName(Type type, Format format)
Definition
TypeNameBuilder.cs:304
System.Reflection.Emit.TypeNameBuilder._str
StringBuilder _str
Definition
TypeNameBuilder.cs:15
System.Reflection.Emit.TypeNameBuilder.PushOpenGenericArgument
void PushOpenGenericArgument()
Definition
TypeNameBuilder.cs:234
System.Reflection.Emit.TypeNameBuilder.ContainsReservedChar
static bool ContainsReservedChar(string name)
Definition
TypeNameBuilder.cs:153
System.Reflection.Emit.TypeNameBuilder.OpenGenericArgument
void OpenGenericArgument()
Definition
TypeNameBuilder.cs:66
System.Reflection.Emit.TypeNameBuilder.AddAssemblySpec
void AddAssemblySpec(string assemblySpec)
Definition
TypeNameBuilder.cs:131
System.Reflection.Emit.TypeNameBuilder.AddElementType
void AddElementType(Type type)
Definition
TypeNameBuilder.cs:280
System.Reflection.Emit.TypeNameBuilder.Append
void Append(char c)
Definition
TypeNameBuilder.cs:264
System.Reflection.Emit.TypeNameBuilder._stackIdx
int _stackIdx
Definition
TypeNameBuilder.cs:29
System.Reflection.Emit.TypeNameBuilder._useAngleBracketsForGenerics
bool _useAngleBracketsForGenerics
Definition
TypeNameBuilder.cs:25
System.Reflection.Emit.TypeNameBuilder._nestedName
bool _nestedName
Definition
TypeNameBuilder.cs:21
System.Reflection.Emit.TypeNameBuilder.ToString
static string ToString(Type type, Format format)
Definition
TypeNameBuilder.cs:269
System.Reflection.Emit.TypeNameBuilder.AddArray
void AddArray(int rank)
Definition
TypeNameBuilder.cs:111
System.Reflection.Emit.TypeNameBuilder.CloseGenericArgument
void CloseGenericArgument()
Definition
TypeNameBuilder.cs:85
System.Reflection.Emit.TypeNameBuilder
Definition
TypeNameBuilder.cs:7
System.Text.StringBuilder.Length
int Length
Definition
StringBuilder.cs:360
System.Text.StringBuilder.Remove
StringBuilder Remove(int startIndex, int length)
Definition
StringBuilder.cs:1049
System.Text.StringBuilder.ToString
override string ToString()
Definition
StringBuilder.cs:679
System.Text.StringBuilder.Append
StringBuilder Append(char value, int repeatCount)
Definition
StringBuilder.cs:744
System.Text.StringBuilder
Definition
StringBuilder.cs:14
System.Type
Definition
Type.cs:14
System.Collections.Generic
Definition
IHashKeyCollection.cs:1
System.Reflection.Emit
Definition
__ExceptionInfo.cs:1
System.Text
Definition
ConsoleEncoding.cs:1
System.ExceptionArgument.list
@ list
System.ExceptionArgument.text
@ text
System.ExceptionArgument.format
@ format
System.ExceptionArgument.type
@ type
System.ExceptionArgument.ch
@ ch
source
System.Private.CoreLib
System.Reflection.Emit
TypeNameBuilder.cs
Generated by
1.10.0