Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
MethodBuilder.cs
Go to the documentation of this file.
5using System.Text;
6
8
9public sealed class MethodBuilder : MethodInfo
10{
11 internal string m_strName;
12
13 private int m_token;
14
15 private readonly ModuleBuilder m_module;
16
17 [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
19
20 private int[] m_mdMethodFixups;
21
22 private byte[] m_localSignature;
23
25
27
28 private byte[] m_ubBody;
29
31
32 internal bool m_bIsBaked;
33
34 private bool m_fInitLocals;
35
37
39
41
43
45
47
49
51
53
55
57
58 private bool m_bIsGenMethDef;
59
60 internal bool m_canBeRuntimeImpl;
61
62 internal bool m_isDllImport;
63
65 {
66 get
67 {
68 if (m_exceptions == null)
69 {
70 return 0;
71 }
72 return m_exceptions.Length;
73 }
74 }
75
76 public override string Name => m_strName;
77
78 public override int MetadataToken => GetToken();
79
81
82 public override Type? DeclaringType
83 {
84 get
85 {
87 {
88 return null;
89 }
90 return m_containingType;
91 }
92 }
93
95
96 public override Type? ReflectedType => DeclaringType;
97
99
101
103 {
104 get
105 {
107 }
108 }
109
110 public override bool IsSecurityCritical => true;
111
112 public override bool IsSecuritySafeCritical => false;
113
114 public override bool IsSecurityTransparent => false;
115
116 public override Type ReturnType => m_returnType;
117
119 {
120 get
121 {
123 {
125 }
127 return method.ReturnParameter;
128 }
129 }
130
132
133 public override bool ContainsGenericParameters
134 {
135 get
136 {
137 throw new NotSupportedException();
138 }
139 }
140
141 public override bool IsGenericMethod => m_inst != null;
142
143 public bool InitLocals
144 {
145 get
146 {
148 return m_fInitLocals;
149 }
150 set
151 {
154 }
155 }
156
157 internal MethodBuilder(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers, ModuleBuilder mod, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TypeBuilder type)
158 {
159 if (name == null)
160 {
161 throw new ArgumentNullException("name");
162 }
163 if (name.Length == 0)
164 {
165 throw new ArgumentException(SR.Argument_EmptyName, "name");
166 }
167 if (name[0] == '\0')
168 {
169 throw new ArgumentException(SR.Argument_IllegalName, "name");
170 }
171 if (mod == null)
172 {
173 throw new ArgumentNullException("mod");
174 }
175 if (parameterTypes != null)
176 {
177 foreach (Type type2 in parameterTypes)
178 {
179 if (type2 == null)
180 {
181 throw new ArgumentNullException("parameterTypes");
182 }
183 }
184 }
185 m_strName = name;
186 m_module = mod;
188 m_returnType = returnType ?? typeof(void);
189 if ((attributes & MethodAttributes.Static) == 0)
190 {
191 callingConvention |= CallingConventions.HasThis;
192 }
193 else if ((attributes & MethodAttributes.Virtual) != 0 && (attributes & MethodAttributes.Abstract) == 0)
194 {
196 }
197 m_callingConvention = callingConvention;
198 if (parameterTypes != null)
199 {
200 m_parameterTypes = new Type[parameterTypes.Length];
201 Array.Copy(parameterTypes, m_parameterTypes, parameterTypes.Length);
202 }
203 else
204 {
205 m_parameterTypes = null;
206 }
207 m_returnTypeRequiredCustomModifiers = returnTypeRequiredCustomModifiers;
208 m_returnTypeOptionalCustomModifiers = returnTypeOptionalCustomModifiers;
209 m_parameterTypeRequiredCustomModifiers = parameterTypeRequiredCustomModifiers;
210 m_parameterTypeOptionalCustomModifiers = parameterTypeOptionalCustomModifiers;
211 m_iAttributes = attributes;
212 m_bIsBaked = false;
213 m_fInitLocals = true;
215 m_ubBody = null;
216 m_ilGenerator = null;
218 }
219
221 {
222 if (il == null)
223 {
224 throw new ArgumentNullException("il");
225 }
226 int num = 0;
227 ModuleBuilder module = m_module;
229 if (m_bIsBaked)
230 {
232 }
233 if (il.m_methodBuilder != this && il.m_methodBuilder != null)
234 {
236 }
238 if (il.m_ScopeTree.m_iOpenScopeCount != 0)
239 {
241 }
242 m_ubBody = il.BakeByteArray();
246 if (num2 > 0)
247 {
248 m_exceptions = new ExceptionHandler[num2];
249 for (int i = 0; i < exceptions.Length; i++)
250 {
251 int[] filterAddresses = exceptions[i].GetFilterAddresses();
252 int[] catchAddresses = exceptions[i].GetCatchAddresses();
253 int[] catchEndAddresses = exceptions[i].GetCatchEndAddresses();
254 Type[] catchClass = exceptions[i].GetCatchClass();
255 int numberOfCatches = exceptions[i].GetNumberOfCatches();
256 int startAddress = exceptions[i].GetStartAddress();
257 int endAddress = exceptions[i].GetEndAddress();
258 int[] exceptionTypes = exceptions[i].GetExceptionTypes();
259 for (int j = 0; j < numberOfCatches; j++)
260 {
261 int exceptionTypeToken = 0;
262 if (catchClass[j] != null)
263 {
264 exceptionTypeToken = module.GetTypeTokenInternal(catchClass[j]);
265 }
266 switch (exceptionTypes[j])
267 {
268 case 0:
269 case 1:
270 case 4:
271 m_exceptions[num++] = new ExceptionHandler(startAddress, endAddress, filterAddresses[j], catchAddresses[j], catchEndAddresses[j], exceptionTypes[j], exceptionTypeToken);
272 break;
273 case 2:
274 m_exceptions[num++] = new ExceptionHandler(startAddress, exceptions[i].GetFinallyEndAddress(), filterAddresses[j], catchAddresses[j], catchEndAddresses[j], exceptionTypes[j], exceptionTypeToken);
275 break;
276 }
277 }
278 }
279 }
280 m_bIsBaked = true;
281 }
282
284 {
285 if (m_bIsBaked)
286 {
287 m_ubBody = null;
288 m_localSymInfo = null;
289 m_mdMethodFixups = null;
290 m_localSignature = null;
291 m_exceptions = null;
292 }
293 }
294
295 internal override Type[] GetParameterTypes()
296 {
298 }
299
301 {
302 if (method is MethodInfo methodInfo)
303 {
304 return methodInfo.ReturnType;
305 }
306 if (method is ConstructorInfo constructorInfo)
307 {
308 return constructorInfo.GetReturnType();
309 }
310 return null;
311 }
312
313 internal void SetToken(int token)
314 {
315 m_token = token;
316 }
317
318 internal byte[] GetBody()
319 {
320 return m_ubBody;
321 }
322
323 internal int[] GetTokenFixups()
324 {
325 return m_mdMethodFixups;
326 }
327
337
338 internal byte[] GetLocalSignature(out int signatureLength)
339 {
340 if (m_localSignature != null)
341 {
342 signatureLength = m_localSignature.Length;
343 return m_localSignature;
344 }
345 if (m_ilGenerator != null && m_ilGenerator.m_localCount != 0)
346 {
347 return m_ilGenerator.m_localSignature.InternalGetSignature(out signatureLength);
348 }
349 return SignatureHelper.GetLocalVarSigHelper(m_module).InternalGetSignature(out signatureLength);
350 }
351
352 internal int GetMaxStack()
353 {
354 if (m_ilGenerator != null)
355 {
357 }
358 return 16;
359 }
360
362 {
363 return m_exceptions;
364 }
365
367 {
368 int num = 0;
369 if (excp == null)
370 {
371 return 0;
372 }
373 for (int i = 0; i < excp.Length; i++)
374 {
375 num += excp[i].GetNumberOfCatches();
376 }
377 return num;
378 }
379
380 internal bool IsTypeCreated()
381 {
382 if (m_containingType != null)
383 {
385 }
386 return false;
387 }
388
389 [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
391 {
392 return m_containingType;
393 }
394
396 {
397 return m_module;
398 }
399
400 public override bool Equals(object? obj)
401 {
402 if (!(obj is MethodBuilder))
403 {
404 return false;
405 }
406 if (!m_strName.Equals(((MethodBuilder)obj).m_strName))
407 {
408 return false;
409 }
411 {
412 return false;
413 }
414 SignatureHelper methodSignature = ((MethodBuilder)obj).GetMethodSignature();
415 if (methodSignature.Equals(GetMethodSignature()))
416 {
417 return true;
418 }
419 return false;
420 }
421
422 public override int GetHashCode()
423 {
424 return m_strName.GetHashCode();
425 }
426
427 public override string ToString()
428 {
429 StringBuilder stringBuilder = new StringBuilder(1000);
430 stringBuilder.Append("Name: ").Append(m_strName).AppendLine(" ");
431 stringBuilder.Append("Attributes: ").Append((int)m_iAttributes).AppendLine();
432 stringBuilder.Append("Method Signature: ").Append(GetMethodSignature()).AppendLine();
433 stringBuilder.AppendLine();
434 return stringBuilder.ToString();
435 }
436
437 public override object Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture)
438 {
440 }
441
446
448 {
449 return this;
450 }
451
452 public override ParameterInfo[] GetParameters()
453 {
455 {
457 }
459 return method.GetParameters();
460 }
461
462 public override object[] GetCustomAttributes(bool inherit)
463 {
465 }
466
467 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
468 {
470 }
471
472 public override bool IsDefined(Type attributeType, bool inherit)
473 {
475 }
476
478 {
479 if (!IsGenericMethod)
480 {
481 throw new InvalidOperationException();
482 }
483 return this;
484 }
485
486 public override Type[] GetGenericArguments()
487 {
488 Type[] inst = m_inst;
489 return inst ?? Type.EmptyTypes;
490 }
491
492 [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
493 public override MethodInfo MakeGenericMethod(params Type[] typeArguments)
494 {
495 return MethodBuilderInstantiation.MakeGenericMethod(this, typeArguments);
496 }
497
499 {
500 if (names == null)
501 {
502 throw new ArgumentNullException("names");
503 }
504 if (names.Length == 0)
505 {
506 throw new ArgumentException(SR.Arg_EmptyArray, "names");
507 }
508 if (m_inst != null)
509 {
511 }
512 for (int i = 0; i < names.Length; i++)
513 {
514 if (names[i] == null)
515 {
516 throw new ArgumentNullException("names");
517 }
518 }
519 if (m_token != 0)
520 {
522 }
523 m_bIsGenMethDef = true;
524 m_inst = new GenericTypeParameterBuilder[names.Length];
525 for (int j = 0; j < names.Length; j++)
526 {
527 m_inst[j] = new GenericTypeParameterBuilder(new TypeBuilder(names[j], j, this));
528 }
529 return m_inst;
530 }
531
532 internal void ThrowIfGeneric()
533 {
535 {
536 throw new InvalidOperationException();
537 }
538 }
539
540 private int GetToken()
541 {
542 if (m_token != 0)
543 {
544 return m_token;
545 }
546 MethodBuilder methodBuilder = null;
547 int result = 0;
549 {
550 if (m_token != 0)
551 {
552 return m_token;
553 }
554 int i;
556 {
557 methodBuilder = m_containingType.m_listMethods[i];
558 result = methodBuilder.GetTokenNoLock();
559 if (methodBuilder == this)
560 {
561 break;
562 }
563 }
564 m_containingType.m_lastTokenizedMethod = i;
565 return result;
566 }
567 }
568
569 private int GetTokenNoLock()
570 {
571 int length;
572 byte[] signature = GetMethodSignature().InternalGetSignature(out length);
573 ModuleBuilder module = m_module;
574 int tkMethod = (m_token = TypeBuilder.DefineMethod(new QCallModule(ref module), m_containingType.MetadataToken, m_strName, signature, length, Attributes));
575 if (m_inst != null)
576 {
578 foreach (GenericTypeParameterBuilder genericTypeParameterBuilder in inst)
579 {
580 if (!genericTypeParameterBuilder.m_type.IsCreated())
581 {
582 genericTypeParameterBuilder.m_type.CreateType();
583 }
584 }
585 }
586 TypeBuilder.SetMethodImpl(new QCallModule(ref module), tkMethod, m_dwMethodImplFlags);
587 return m_token;
588 }
589
590 public void SetParameters(params Type[] parameterTypes)
591 {
592 AssemblyBuilder.CheckContext(parameterTypes);
593 SetSignature(null, null, null, parameterTypes, null, null);
594 }
595
596 public void SetReturnType(Type? returnType)
597 {
598 AssemblyBuilder.CheckContext(returnType);
599 SetSignature(returnType, null, null, null, null, null);
600 }
601
602 public void SetSignature(Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers)
603 {
604 if (m_token == 0)
605 {
606 AssemblyBuilder.CheckContext(returnType);
607 AssemblyBuilder.CheckContext(returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes);
608 AssemblyBuilder.CheckContext(parameterTypeRequiredCustomModifiers);
609 AssemblyBuilder.CheckContext(parameterTypeOptionalCustomModifiers);
611 if (returnType != null)
612 {
613 m_returnType = returnType;
614 }
615 if (parameterTypes != null)
616 {
617 m_parameterTypes = new Type[parameterTypes.Length];
618 Array.Copy(parameterTypes, m_parameterTypes, parameterTypes.Length);
619 }
620 m_returnTypeRequiredCustomModifiers = returnTypeRequiredCustomModifiers;
621 m_returnTypeOptionalCustomModifiers = returnTypeOptionalCustomModifiers;
622 m_parameterTypeRequiredCustomModifiers = parameterTypeRequiredCustomModifiers;
623 m_parameterTypeOptionalCustomModifiers = parameterTypeOptionalCustomModifiers;
624 }
625 }
626
627 public ParameterBuilder DefineParameter(int position, ParameterAttributes attributes, string? strParamName)
628 {
629 if (position < 0)
630 {
632 }
635 if (position > 0 && (m_parameterTypes == null || position > m_parameterTypes.Length))
636 {
638 }
639 attributes &= ~ParameterAttributes.ReservedMask;
640 return new ParameterBuilder(this, position, attributes, strParamName);
641 }
642
644 {
647 m_dwMethodImplFlags = attributes;
648 m_canBeRuntimeImpl = true;
649 ModuleBuilder module = m_module;
650 TypeBuilder.SetMethodImpl(new QCallModule(ref module), MetadataToken, attributes);
651 }
652
654 {
657 return m_ilGenerator ?? (m_ilGenerator = new ILGenerator(this));
658 }
659
661 {
664 return m_ilGenerator ?? (m_ilGenerator = new ILGenerator(this, size));
665 }
666
668 {
669 if ((m_dwMethodImplFlags & MethodImplAttributes.CodeTypeMask) != 0 || (m_dwMethodImplFlags & MethodImplAttributes.ManagedMask) != 0 || (m_iAttributes & MethodAttributes.PinvokeImpl) != 0 || m_isDllImport)
670 {
672 }
673 }
674
675 internal Module GetModule()
676 {
677 return GetModuleBuilder();
678 }
679
680 public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
681 {
682 if ((object)con == null)
683 {
684 throw new ArgumentNullException("con");
685 }
686 if (binaryAttribute == null)
687 {
688 throw new ArgumentNullException("binaryAttribute");
689 }
692 if (IsKnownCA(con))
693 {
694 ParseCA(con);
695 }
696 }
697
699 {
700 if (customBuilder == null)
701 {
702 throw new ArgumentNullException("customBuilder");
703 }
706 if (IsKnownCA(customBuilder.m_con))
707 {
708 ParseCA(customBuilder.m_con);
709 }
710 }
711
712 private static bool IsKnownCA(ConstructorInfo con)
713 {
714 Type declaringType = con.DeclaringType;
715 if (!(declaringType == typeof(MethodImplAttribute)))
716 {
717 return declaringType == typeof(DllImportAttribute);
718 }
719 return true;
720 }
721
722 private void ParseCA(ConstructorInfo con)
723 {
724 Type declaringType = con.DeclaringType;
725 if (declaringType == typeof(MethodImplAttribute))
726 {
727 m_canBeRuntimeImpl = true;
728 }
729 else if (declaringType == typeof(DllImportAttribute))
730 {
731 m_canBeRuntimeImpl = true;
732 m_isDllImport = true;
733 }
734 }
735}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
static void CheckContext(params Type[][] typess)
void CreateCustomAttribute(ModuleBuilder mod, int tkOwner)
__ExceptionInfo[] GetExceptions()
static MethodInfo MakeGenericMethod(MethodInfo method, Type[] inst)
MethodImplAttributes m_dwMethodImplFlags
void SetImplementationFlags(MethodImplAttributes attributes)
static bool IsKnownCA(ConstructorInfo con)
GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names)
void SetSignature(Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers)
override object[] GetCustomAttributes(Type attributeType, bool inherit)
override MethodInfo GetBaseDefinition()
override RuntimeMethodHandle MethodHandle
void SetCustomAttribute(CustomAttributeBuilder customBuilder)
static Type GetMethodBaseReturnType(MethodBase method)
override ICustomAttributeProvider ReturnTypeCustomAttributes
override MethodInfo MakeGenericMethod(params Type[] typeArguments)
byte[] GetLocalSignature(out int signatureLength)
override ParameterInfo[] GetParameters()
override ParameterInfo ReturnParameter
void CreateMethodBodyHelper(ILGenerator il)
void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
override MethodAttributes Attributes
override bool Equals(object? obj)
GenericTypeParameterBuilder[] m_inst
MethodBuilder(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers, ModuleBuilder mod, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TypeBuilder type)
ParameterBuilder DefineParameter(int position, ParameterAttributes attributes, string? strParamName)
override object Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture)
void SetReturnType(Type? returnType)
static int CalculateNumberOfExceptions(__ExceptionInfo[] excp)
void ParseCA(ConstructorInfo con)
override MethodInfo GetGenericMethodDefinition()
override MethodImplAttributes GetMethodImplementationFlags()
void SetParameters(params Type[] parameterTypes)
override bool IsDefined(Type attributeType, bool inherit)
override object[] GetCustomAttributes(bool inherit)
readonly MethodAttributes m_iAttributes
readonly CallingConventions m_callingConvention
ExceptionHandler[] GetExceptionHandlers()
int GetConstructorToken(ConstructorInfo con)
byte[] InternalGetSignature(out int length)
static SignatureHelper GetLocalVarSigHelper()
static SignatureHelper GetMethodSigHelper(Module? mod, Type? returnType, Type[]? parameterTypes)
static void SetMethodImpl(QCallModule module, int tkMethod, MethodImplAttributes MethodImplAttributes)
static int DefineMethod(QCallModule module, int tkParent, string name, byte[] signature, int sigLength, MethodAttributes attributes)
static void DefineCustomAttribute(QCallModule module, int tkAssociate, int tkConstructor, byte[] attr, int attrLength)
List< MethodBuilder > m_listMethods
static MethodInfo GetMethod(Type type, MethodInfo method)
ParameterInfo[] GetParameters()
virtual ParameterInfo ReturnParameter
Definition MethodInfo.cs:11
static string InvalidOperation_MethodHasBody
Definition SR.cs:1466
static string Arg_NoStaticVirtual
Definition SR.cs:340
static string ArgumentOutOfRange_ParamSequence
Definition SR.cs:1088
static string InvalidOperation_GenericParametersAlreadySet
Definition SR.cs:1448
static string InvalidOperation_MethodBuilderBaked
Definition SR.cs:1464
static string InvalidOperation_BadILGeneratorUsage
Definition SR.cs:1402
static string NotSupported_DynamicModule
Definition SR.cs:1672
static string Argument_IllegalName
Definition SR.cs:630
static string InvalidOperation_TypeNotCreated
Definition SR.cs:1526
static string InvalidOperation_ShouldNotHaveMethodBody
Definition SR.cs:1516
static string InvalidOperation_OpenLocalVariableScope
Definition SR.cs:1498
static string Arg_EmptyArray
Definition SR.cs:134
static string Argument_EmptyName
Definition SR.cs:584
Definition SR.cs:7
override string ToString()
StringBuilder Append(char value, int repeatCount)
static readonly Type[] EmptyTypes
Definition Type.cs:19