Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ReflectionAwareCodeGen.cs
Go to the documentation of this file.
5using System.Text;
6
8
9internal sealed class ReflectionAwareCodeGen
10{
12
14
15 private readonly IndentedWriter _writer;
16
21
22 [RequiresUnreferencedCode("calls GetTypeDesc")]
23 internal void WriteReflectionInit(TypeScope scope)
24 {
25 foreach (Type type in scope.Types)
26 {
27 TypeDesc typeDesc = scope.GetTypeDesc(type);
28 if (typeDesc.UseReflection)
29 {
31 }
32 }
33 }
34
35 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
37 {
39 string cSharpName = typeDesc.CSharpName;
41 if (text != null)
42 {
43 return text;
44 }
45 if (type.IsArray)
46 {
47 text = GenerateVariableName("array", typeDesc.CSharpName);
49 if (arrayElementTypeDesc.UseReflection)
50 {
52 _writer.WriteLine("static " + typeof(Type).FullName + " " + text + " = " + text2 + ".MakeArrayType();");
53 }
54 else
55 {
57 _writer.Write("static " + typeof(Type).FullName + " " + text + " = " + text3 + ".GetType(");
59 _writer.WriteLine(");");
60 }
61 }
62 else
63 {
64 text = GenerateVariableName("type", typeDesc.CSharpName);
66 if (underlyingType != null)
67 {
69 _writer.WriteLine("static " + typeof(Type).FullName + " " + text + " = typeof(System.Nullable<>).MakeGenericType(new " + typeof(Type).FullName + "[] {" + text4 + "});");
70 }
71 else
72 {
74 _writer.Write("static " + typeof(Type).FullName + " " + text + " = " + text5 + ".GetType(");
76 _writer.WriteLine(");");
77 }
78 }
81 if (typeMappingFromTypeDesc != null)
82 {
84 }
85 if (typeDesc.IsCollection || typeDesc.IsEnumerable)
86 {
88 if (arrayElementTypeDesc2.UseReflection)
89 {
91 }
93 }
94 return text;
95 }
96
97 [MemberNotNull("_reflectionVariables")]
98 private void InitTheFirstTime()
99 {
100 if (_reflectionVariables == null)
101 {
103 _writer.Write(string.Format(CultureInfo.InvariantCulture, "\r\n sealed class XSFieldInfo {{\r\n {3} fieldInfo;\r\n public XSFieldInfo({2} t, {1} memberName){{\r\n fieldInfo = t.GetField(memberName);\r\n }}\r\n public {0} this[{0} o] {{\r\n get {{\r\n return fieldInfo.GetValue(o);\r\n }}\r\n set {{\r\n fieldInfo.SetValue(o, value);\r\n }}\r\n }}\r\n\r\n }}\r\n sealed class XSPropInfo {{\r\n {4} propInfo;\r\n public XSPropInfo({2} t, {1} memberName){{\r\n propInfo = t.GetProperty(memberName);\r\n }}\r\n public {0} this[{0} o] {{\r\n get {{\r\n return propInfo.GetValue(o, null);\r\n }}\r\n set {{\r\n propInfo.SetValue(o, value, null);\r\n }}\r\n }}\r\n }}\r\n sealed class XSArrayInfo {{\r\n {4} propInfo;\r\n public XSArrayInfo({4} propInfo){{\r\n this.propInfo = propInfo;\r\n }}\r\n public {0} this[{0} a, int i] {{\r\n get {{\r\n return propInfo.GetValue(a, new {0}[]{{i}});\r\n }}\r\n set {{\r\n propInfo.SetValue(a, value, new {0}[]{{i}});\r\n }}\r\n }}\r\n }}\r\n", "object", "string", typeof(Type).FullName, typeof(FieldInfo).FullName, typeof(PropertyInfo).FullName));
105 }
106 }
107
109 {
110 string cSharpName = mapping.TypeDesc.CSharpName;
112 {
114 for (int i = 0; i < structMapping.Members.Length; i++)
115 {
118 if (memberMapping.CheckShouldPersist)
119 {
120 string memberName = "ShouldSerialize" + memberMapping.Name;
122 }
123 if (memberMapping.CheckSpecified != 0)
124 {
125 string memberName2 = memberMapping.Name + "Specified";
127 }
128 if (memberMapping.ChoiceIdentifier != null)
129 {
130 string memberName3 = memberMapping.ChoiceIdentifier.MemberName;
132 }
133 }
134 }
135 else if (mapping is EnumMapping)
136 {
137 FieldInfo[] fields = type.GetFields();
138 for (int j = 0; j < fields.Length; j++)
139 {
141 }
142 }
143 }
144
146 {
148 string cSharpName2 = typeDesc.ArrayElementTypeDesc.CSharpName;
149 bool useReflection = typeDesc.ArrayElementTypeDesc.UseReflection;
150 if (typeDesc.IsCollection)
151 {
153 }
154 else if (typeDesc.IsEnumerable)
155 {
156 if (typeDesc.IsGenericInterface)
157 {
158 WriteMethodInfo(cSharpName, typeVariable, "System.Collections.Generic.IEnumerable*", true);
159 }
160 else if (!typeDesc.IsPrivateImplementation)
161 {
162 WriteMethodInfo(cSharpName, typeVariable, "GetEnumerator", true);
163 }
164 }
166 }
167
169 {
170 string fullName = type.Assembly.FullName;
172 if (text == null)
173 {
174 int num = fullName.IndexOf(',');
175 string fullName2 = ((num > -1) ? fullName.Substring(0, num) : fullName);
176 text = GenerateVariableName("assembly", fullName2);
177 _writer.Write("static " + typeof(Assembly).FullName + " " + text + " = ResolveDynamicAssembly(");
179 _writer.WriteLine(");");
181 }
182 return text;
183 }
184
186 {
187 MemberInfo[] member = type.GetMember(memberName);
188 for (int i = 0; i < member.Length; i++)
189 {
190 if (member[i] is PropertyInfo)
191 {
192 string text = GenerateVariableName("prop", memberName);
193 _writer.Write("static XSPropInfo " + text + " = new XSPropInfo(" + typeVariable + ", ");
195 _writer.WriteLine(");");
197 return text;
198 }
199 if (member[i] is FieldInfo)
200 {
201 string text2 = GenerateVariableName("field", memberName);
202 _writer.Write("static XSFieldInfo " + text2 + " = new XSFieldInfo(" + typeVariable + ", ");
204 _writer.WriteLine(");");
206 return text2;
207 }
208 }
210 }
211
212 private string WriteMethodInfo(string escapedName, string typeVariable, string memberName, bool isNonPublic, params string[] paramTypes)
213 {
214 string text = GenerateVariableName("method", memberName);
215 _writer.Write("static " + typeof(MethodInfo).FullName + " " + text + " = " + typeVariable + ".GetMethod(");
217 _writer.Write(", ");
218 string fullName = typeof(BindingFlags).FullName;
220 _writer.Write(".Public | ");
222 _writer.Write(".Instance | ");
224 _writer.Write(".Static");
225 if (isNonPublic)
226 {
227 _writer.Write(" | ");
229 _writer.Write(".NonPublic");
230 }
231 _writer.Write(", null, ");
232 _writer.Write("new " + typeof(Type).FullName + "[] { ");
233 for (int i = 0; i < paramTypes.Length; i++)
234 {
236 if (i < paramTypes.Length - 1)
237 {
238 _writer.Write(", ");
239 }
240 }
241 _writer.WriteLine("}, null);");
243 return text;
244 }
245
264
265 private string GenerateVariableName(string prefix, string fullName)
266 {
269 }
270
271 internal string GetReflectionVariable(string typeFullName, string memberName)
272 {
273 string key = ((memberName != null) ? (memberName + ":" + typeFullName) : typeFullName);
274 return (string)_reflectionVariables[key];
275 }
276
277 internal string GetStringForMethodInvoke(string obj, string escapedTypeName, string methodName, bool useReflection, params string[] args)
278 {
280 if (useReflection)
281 {
283 stringBuilder.Append(".Invoke(");
284 stringBuilder.Append(obj);
285 stringBuilder.Append(", new object[] {");
286 }
287 else
288 {
289 stringBuilder.Append(obj);
290 stringBuilder.Append(".@");
292 stringBuilder.Append('(');
293 }
294 for (int i = 0; i < args.Length; i++)
295 {
296 if (i != 0)
297 {
298 stringBuilder.Append(", ");
299 }
300 stringBuilder.Append(args[i]);
301 }
302 if (useReflection)
303 {
304 stringBuilder.Append("})");
305 }
306 else
307 {
308 stringBuilder.Append(')');
309 }
310 return stringBuilder.ToString();
311 }
312
323
325 {
326 if (useReflection)
327 {
328 return typeof(Convert).FullName + ".ToInt64(" + variable + ")";
329 }
330 return "((" + typeof(long).FullName + ")" + variable + ")";
331 }
332
333 internal string GetStringForTypeof(string typeFullName, bool useReflection)
334 {
335 if (useReflection)
336 {
338 }
339 return "typeof(" + typeFullName + ")";
340 }
341
342 internal string GetStringForMember(string obj, string memberName, TypeDesc typeDesc)
343 {
344 if (!typeDesc.UseReflection)
345 {
346 return obj + ".@" + memberName;
347 }
348 while (typeDesc != null)
349 {
350 string cSharpName = typeDesc.CSharpName;
352 if (reflectionVariable != null)
353 {
354 return reflectionVariable + "[" + obj + "]";
355 }
356 typeDesc = typeDesc.BaseTypeDesc;
357 if (typeDesc != null && !typeDesc.UseReflection)
358 {
359 return "((" + typeDesc.CSharpName + ")" + obj + ").@" + memberName;
360 }
361 }
362 return "[" + obj + "]";
363 }
364
365 internal string GetStringForEnumMember(string typeFullName, string memberName, bool useReflection)
366 {
367 if (!useReflection)
368 {
369 return typeFullName + ".@" + memberName;
370 }
372 return reflectionVariable + "[null]";
373 }
374
376 {
377 if (!arrayTypeDesc.UseReflection)
378 {
379 return arrayName + "[" + subscript + "]";
380 }
383 return reflectionVariable + "[" + arrayName + ", " + subscript + "]";
384 }
385
386 internal string GetStringForMethod(string obj, string typeFullName, string memberName, bool useReflection)
387 {
388 if (!useReflection)
389 {
390 return obj + "." + memberName + "(";
391 }
393 return reflectionVariable + ".Invoke(" + obj + ", new object[]{";
394 }
395
400
401 internal string GetStringForCreateInstance(string escapedTypeName, bool useReflection, bool ctorInaccessible, bool cast, string arg)
402 {
404 {
405 return "new " + escapedTypeName + "(" + arg + ")";
406 }
408 }
409
410 internal string GetStringForCreateInstance(string type, string cast, bool nonPublic, string arg)
411 {
413 if (cast != null && cast.Length > 0)
414 {
415 stringBuilder.Append('(');
416 stringBuilder.Append(cast);
417 stringBuilder.Append(')');
418 }
420 stringBuilder.Append(".CreateInstance(");
421 stringBuilder.Append(type);
422 stringBuilder.Append(", ");
423 string fullName = typeof(BindingFlags).FullName;
424 stringBuilder.Append(fullName);
425 stringBuilder.Append(".Instance | ");
426 stringBuilder.Append(fullName);
427 stringBuilder.Append(".Public | ");
428 stringBuilder.Append(fullName);
429 stringBuilder.Append(".CreateInstance");
430 if (nonPublic)
431 {
432 stringBuilder.Append(" | ");
433 stringBuilder.Append(fullName);
434 stringBuilder.Append(".NonPublic");
435 }
436 if (arg == null || arg.Length == 0)
437 {
438 stringBuilder.Append(", null, new object[0], null)");
439 }
440 else
441 {
442 stringBuilder.Append(", null, new object[] { ");
443 stringBuilder.Append(arg);
444 stringBuilder.Append(" }, null)");
445 }
446 return stringBuilder.ToString();
447 }
448
449 internal void WriteLocalDecl(string typeFullName, string variableName, string initValue, bool useReflection)
450 {
451 if (useReflection)
452 {
453 typeFullName = "object";
454 }
456 _writer.Write(" ");
458 if (initValue != null)
459 {
460 _writer.Write(" = ");
461 if (!useReflection && initValue != "null")
462 {
463 _writer.Write("(" + typeFullName + ")");
464 }
466 }
467 _writer.WriteLine(";");
468 }
469
479
480 internal void WriteInstanceOf(string source, string escapedTypeName, bool useReflection)
481 {
482 if (!useReflection)
483 {
485 _writer.Write(" is ");
487 }
488 else
489 {
491 _writer.Write(".IsAssignableFrom(");
493 _writer.Write(".GetType())");
494 }
495 }
496
497 internal void WriteArrayLocalDecl(string typeName, string variableName, string initValue, TypeDesc arrayTypeDesc)
498 {
499 if (arrayTypeDesc.UseReflection)
500 {
501 typeName = (arrayTypeDesc.IsEnumerable ? typeof(IEnumerable).FullName : ((!arrayTypeDesc.IsCollection) ? typeof(Array).FullName : typeof(ICollection).FullName));
502 }
503 _writer.Write(typeName);
504 _writer.Write(" ");
506 if (initValue != null)
507 {
508 _writer.Write(" = ");
509 if (initValue != "null")
510 {
511 _writer.Write("(" + typeName + ")");
512 }
514 }
515 _writer.WriteLine(";");
516 }
517
519 {
520 _writer.Write("case ");
521 if (useReflection)
522 {
524 }
525 else
526 {
528 _writer.Write(".@");
530 _writer.Write(c.Name);
531 }
532 _writer.Write(": ");
533 }
534
541
543 {
544 if (!useReflection)
545 {
547 _writer.Write(" == typeof(");
549 _writer.Write(")");
550 }
551 else
552 {
554 _writer.Write(".IsArray ");
555 _writer.Write(" && ");
557 }
558 }
559
561 {
562 if (value == null)
563 {
564 writer.Write("null");
565 return;
566 }
567 writer.Write("@\"");
568 foreach (char c in value)
569 {
570 if (c < ' ')
571 {
572 switch (c)
573 {
574 case '\r':
575 writer.Write("\\r");
576 continue;
577 case '\n':
578 writer.Write("\\n");
579 continue;
580 case '\t':
581 writer.Write("\\t");
582 continue;
583 }
584 byte b = (byte)c;
585 writer.Write("\\x");
588 }
589 else if (c == '"')
590 {
591 writer.Write("\"\"");
592 }
593 else
594 {
595 writer.Write(c);
596 }
597 }
598 writer.Write("\"");
599 }
600
601 internal void WriteQuotedCSharpString(string value)
602 {
604 }
605}
virtual void Add(object key, object? value)
Definition Hashtable.cs:676
static CultureInfo InvariantCulture
static char ToCharUpper(int value)
static string XmlSerializerUnsupportedType
Definition SR.cs:1394
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
Definition SR.cs:7
static void CheckValidIdentifier([NotNull] string ident)
static string MakeValidInternal(string identifier)
static string GetCSharpName(string name)
void WriteArrayLocalDecl(string typeName, string variableName, string initValue, TypeDesc arrayTypeDesc)
void WriteEnumCase(string fullTypeName, ConstantMapping c, bool useReflection)
string GetStringForTypeof(string typeFullName, bool useReflection)
string GetStringForMethod(string obj, string typeFullName, string memberName, bool useReflection)
string WriteMemberInfo([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.PublicMethods|DynamicallyAccessedMemberTypes.PublicFields|DynamicallyAccessedMemberTypes.PublicNestedTypes|DynamicallyAccessedMemberTypes.PublicProperties|DynamicallyAccessedMemberTypes.PublicEvents)] Type type, string escapedName, string typeVariable, string memberName)
void WriteCreateInstance(string escapedName, string source, bool useReflection, bool ctorInaccessible)
string GetStringForCreateInstance(string escapedTypeName, bool useReflection, bool ctorInaccessible, bool cast)
static void WriteQuotedCSharpString(IndentedWriter writer, string value)
void WriteLocalDecl(string typeFullName, string variableName, string initValue, bool useReflection)
void WriteCollectionInfo(string typeVariable, TypeDesc typeDesc, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.PublicMethods|DynamicallyAccessedMemberTypes.PublicFields|DynamicallyAccessedMemberTypes.PublicNestedTypes|DynamicallyAccessedMemberTypes.PublicProperties|DynamicallyAccessedMemberTypes.PublicEvents)] Type type)
string WriteDefaultIndexerInit([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.PublicMethods|DynamicallyAccessedMemberTypes.PublicFields|DynamicallyAccessedMemberTypes.PublicNestedTypes|DynamicallyAccessedMemberTypes.PublicProperties|DynamicallyAccessedMemberTypes.PublicEvents)] Type type, string escapedName, bool collectionUseReflection, bool elementUseReflection)
string WriteTypeInfo(TypeScope scope, TypeDesc typeDesc, Type type)
string GetStringForEnumMember(string typeFullName, string memberName, bool useReflection)
string WriteMethodInfo(string escapedName, string typeVariable, string memberName, bool isNonPublic, params string[] paramTypes)
void WriteTypeCompare(string variable, string escapedTypeName, bool useReflection)
string GetStringForEnumLongValue(string variable, bool useReflection)
string GenerateVariableName(string prefix, string fullName)
string GetStringForEnumCompare(EnumMapping mapping, string memberName, bool useReflection)
void WriteInstanceOf(string source, string escapedTypeName, bool useReflection)
string GetStringForMethodInvoke(string obj, string escapedTypeName, string methodName, bool useReflection, params string[] args)
string GetStringForCreateInstance(string escapedTypeName, bool useReflection, bool ctorInaccessible, bool cast, string arg)
string GetStringForArrayMember(string arrayName, string subscript, TypeDesc arrayTypeDesc)
string GetStringForCreateInstance(string type, string cast, bool nonPublic, string arg)
string GetStringForMember(string obj, string memberName, TypeDesc typeDesc)
void WriteArrayTypeCompare(string variable, string escapedTypeName, string elementTypeName, bool useReflection)
string GetReflectionVariable(string typeFullName, string memberName)
void WriteMappingInfo(TypeMapping mapping, string typeVariable, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.PublicMethods|DynamicallyAccessedMemberTypes.PublicFields|DynamicallyAccessedMemberTypes.PublicNestedTypes|DynamicallyAccessedMemberTypes.PublicProperties|DynamicallyAccessedMemberTypes.PublicEvents)] Type type)
Type GetTypeFromTypeDesc(TypeDesc typeDesc)
Definition TypeScope.cs:308
static PropertyInfo GetDefaultIndexer([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.PublicMethods|DynamicallyAccessedMemberTypes.PublicFields|DynamicallyAccessedMemberTypes.PublicNestedTypes|DynamicallyAccessedMemberTypes.PublicProperties|DynamicallyAccessedMemberTypes.PublicEvents)] Type type, string memberInfo)
Definition TypeScope.cs:848
TypeMapping GetTypeMappingFromTypeDesc(TypeDesc typeDesc)
Definition TypeScope.cs:296
TypeDesc GetTypeDesc(string name, string ns)
Definition TypeScope.cs:224
static ? Type GetUnderlyingType(Type nullableType)
Definition Nullable.cs:139