Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DynamicMethod.cs
Go to the documentation of this file.
4using System.Text;
6
8
9public sealed class DynamicMethod : MethodInfo
10{
11 internal sealed class RTDynamicMethod : MethodInfo
12 {
14
16
17 private string m_name;
18
20
22
23 public override string Name => m_name;
24
25 public override Type DeclaringType => null;
26
27 public override Type ReflectedType => null;
28
29 public override Module Module => m_owner.m_module;
30
38
40
41 public override CallingConventions CallingConvention => m_callingConvention;
42
44
46
48
49 public override Type ReturnType => m_owner.m_returnType;
50
52
54
55 internal RTDynamicMethod(DynamicMethod owner, string name, MethodAttributes attributes, CallingConventions callingConvention)
56 {
57 m_owner = owner;
58 m_name = name;
59 m_attributes = attributes;
60 m_callingConvention = callingConvention;
61 }
62
63 public override string ToString()
64 {
65 ValueStringBuilder sbParamList = new ValueStringBuilder(100);
66 sbParamList.Append(ReturnType.FormatTypeName());
67 sbParamList.Append(' ');
68 sbParamList.Append(Name);
69 sbParamList.Append('(');
70 MethodBase.AppendParameters(ref sbParamList, GetParameterTypes(), CallingConvention);
71 sbParamList.Append(')');
72 return sbParamList.ToString();
73 }
74
75 public override MethodInfo GetBaseDefinition()
76 {
77 return this;
78 }
79
80 public override ParameterInfo[] GetParameters()
81 {
83 ParameterInfo[] array2 = array;
84 ParameterInfo[] array3 = new ParameterInfo[array2.Length];
85 Array.Copy(array2, array3, array2.Length);
86 return array3;
87 }
88
90 {
91 return MethodImplAttributes.NoInlining;
92 }
93
94 public override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
95 {
97 }
98
99 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
100 {
101 if (attributeType == null)
102 {
103 throw new ArgumentNullException("attributeType");
104 }
105 if (attributeType.IsAssignableFrom(typeof(MethodImplAttribute)))
106 {
107 return new object[1]
108 {
110 };
111 }
112 return Array.Empty<object>();
113 }
114
115 public override object[] GetCustomAttributes(bool inherit)
116 {
117 return new object[1]
118 {
120 };
121 }
122
123 public override bool IsDefined(Type attributeType, bool inherit)
124 {
125 if (attributeType == null)
126 {
127 throw new ArgumentNullException("attributeType");
128 }
129 if (attributeType.IsAssignableFrom(typeof(MethodImplAttribute)))
130 {
131 return true;
132 }
133 return false;
134 }
135
137 {
138 if (m_parameters == null)
139 {
140 Type[] parameterTypes = m_owner.m_parameterTypes;
141 Type[] array = parameterTypes;
142 RuntimeParameterInfo[] array2 = new RuntimeParameterInfo[array.Length];
143 for (int i = 0; i < array.Length; i++)
144 {
145 array2[i] = new RuntimeParameterInfo(this, null, array[i], i);
146 }
147 if (m_parameters == null)
148 {
149 m_parameters = array2;
150 }
151 }
152 return m_parameters;
153 }
154 }
155
157
159
161
163
165
166 private bool m_fInitLocals;
167
169
170 internal bool m_skipVisibility;
171
173
175
177
179
181
182 private static readonly object s_anonymouslyHostedDynamicMethodsModuleLock = new object();
183
184 public override string Name => m_dynMethod.Name;
185
187
189
190 public override Module Module => m_dynMethod.Module;
191
193 {
194 get
195 {
197 }
198 }
199
201
202 public override CallingConventions CallingConvention => m_dynMethod.CallingConvention;
203
204 public override bool IsSecurityCritical => true;
205
206 public override bool IsSecuritySafeCritical => false;
207
208 public override bool IsSecurityTransparent => false;
209
211
213
215
216 public bool InitLocals
217 {
218 get
219 {
220 return m_fInitLocals;
221 }
222 set
223 {
225 }
226 }
227
228 public DynamicMethod(string name, Type? returnType, Type[]? parameterTypes)
229 {
230 Init(name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, null, null, skipVisibility: false, transparentMethod: true);
231 }
232
233 public DynamicMethod(string name, Type? returnType, Type[]? parameterTypes, bool restrictedSkipVisibility)
234 {
235 Init(name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, null, null, restrictedSkipVisibility, transparentMethod: true);
236 }
237
238 public DynamicMethod(string name, Type? returnType, Type[]? parameterTypes, Module m)
239 {
240 if (m == null)
241 {
242 throw new ArgumentNullException("m");
243 }
244 Init(name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, null, m, skipVisibility: false, transparentMethod: false);
245 }
246
247 public DynamicMethod(string name, Type? returnType, Type[]? parameterTypes, Module m, bool skipVisibility)
248 {
249 if (m == null)
250 {
251 throw new ArgumentNullException("m");
252 }
253 Init(name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, null, m, skipVisibility, transparentMethod: false);
254 }
255
256 public DynamicMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes, Module m, bool skipVisibility)
257 {
258 if (m == null)
259 {
260 throw new ArgumentNullException("m");
261 }
262 Init(name, attributes, callingConvention, returnType, parameterTypes, null, m, skipVisibility, transparentMethod: false);
263 }
264
265 public DynamicMethod(string name, Type? returnType, Type[]? parameterTypes, Type owner)
266 {
267 if (owner == null)
268 {
269 throw new ArgumentNullException("owner");
270 }
271 Init(name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, owner, null, skipVisibility: false, transparentMethod: false);
272 }
273
274 public DynamicMethod(string name, Type? returnType, Type[]? parameterTypes, Type owner, bool skipVisibility)
275 {
276 if (owner == null)
277 {
278 throw new ArgumentNullException("owner");
279 }
280 Init(name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, owner, null, skipVisibility, transparentMethod: false);
281 }
282
283 public DynamicMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes, Type owner, bool skipVisibility)
284 {
285 if (owner == null)
286 {
287 throw new ArgumentNullException("owner");
288 }
289 Init(name, attributes, callingConvention, returnType, parameterTypes, owner, null, skipVisibility, transparentMethod: false);
290 }
291
292 private static void CheckConsistency(MethodAttributes attributes, CallingConventions callingConvention)
293 {
294 if ((attributes & ~MethodAttributes.MemberAccessMask) != MethodAttributes.Static)
295 {
297 }
298 if ((attributes & MethodAttributes.MemberAccessMask) != MethodAttributes.Public)
299 {
301 }
302 if (callingConvention != CallingConventions.Standard && callingConvention != CallingConventions.VarArgs)
303 {
305 }
306 if (callingConvention == CallingConventions.VarArgs)
307 {
309 }
310 }
311
313 {
315 {
317 }
319 {
321 {
323 }
324 AssemblyName name = new AssemblyName("Anonymously Hosted DynamicMethods Assembly");
325 StackCrawlMark stackMark = StackCrawlMark.LookForMe;
326 AssemblyBuilder assemblyBuilder = AssemblyBuilder.InternalDefineDynamicAssembly(name, AssemblyBuilderAccess.Run, ref stackMark, null, null);
328 }
330 }
331
332 [MemberNotNull("m_parameterTypes")]
333 [MemberNotNull("m_returnType")]
334 [MemberNotNull("m_dynMethod")]
335 private void Init(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] signature, Type owner, Module m, bool skipVisibility, bool transparentMethod)
336 {
337 CheckConsistency(attributes, callingConvention);
338 if (signature != null)
339 {
340 m_parameterTypes = new RuntimeType[signature.Length];
341 for (int i = 0; i < signature.Length; i++)
342 {
343 if (signature[i] == null)
344 {
346 }
348 if (m_parameterTypes[i] == null || m_parameterTypes[i] == typeof(void))
349 {
351 }
352 }
353 }
354 else
355 {
357 }
358 m_returnType = ((returnType == null) ? ((RuntimeType)typeof(void)) : (returnType.UnderlyingSystemType as RuntimeType));
359 if (m_returnType == null)
360 {
362 }
363 if (transparentMethod)
364 {
366 if (skipVisibility)
367 {
369 }
370 }
371 else
372 {
373 if (m != null)
374 {
376 }
377 else
378 {
379 RuntimeType runtimeType = null;
380 if (owner != null)
381 {
382 runtimeType = owner.UnderlyingSystemType as RuntimeType;
383 }
384 if (runtimeType != null)
385 {
386 if (runtimeType.HasElementType || runtimeType.ContainsGenericParameters || runtimeType.IsGenericParameter || runtimeType.IsInterface)
387 {
389 }
390 m_typeOwner = runtimeType;
391 m_module = runtimeType.GetRuntimeModule();
392 }
393 }
394 m_skipVisibility = skipVisibility;
395 }
396 m_ilGenerator = null;
397 m_fInitLocals = true;
398 m_methodHandle = null;
399 if (name == null)
400 {
401 throw new ArgumentNullException("name");
402 }
403 m_dynMethod = new RTDynamicMethod(this, name, attributes, callingConvention);
404 }
405
406 public sealed override Delegate CreateDelegate(Type delegateType)
407 {
409 {
411 IRuntimeMethodInfo methodHandle = m_methodHandle;
413 GC.KeepAlive(methodHandle);
414 }
416 multicastDelegate.StoreDynamicMethod(GetMethodInfo());
417 return multicastDelegate;
418 }
419
420 public sealed override Delegate CreateDelegate(Type delegateType, object? target)
421 {
423 {
425 IRuntimeMethodInfo methodHandle = m_methodHandle;
427 GC.KeepAlive(methodHandle);
428 }
430 multicastDelegate.StoreDynamicMethod(GetMethodInfo());
431 return multicastDelegate;
432 }
433
435 {
436 if (m_methodHandle == null)
437 {
438 lock (this)
439 {
440 if (m_methodHandle == null)
441 {
442 if (m_DynamicILInfo != null)
443 {
445 }
446 else
447 {
448 if (m_ilGenerator == null || m_ilGenerator.ILOffset == 0)
449 {
451 }
453 }
454 }
455 }
456 }
458 }
459
460 public override string ToString()
461 {
462 return m_dynMethod.ToString();
463 }
464
466 {
467 return this;
468 }
469
470 public override ParameterInfo[] GetParameters()
471 {
472 return m_dynMethod.GetParameters();
473 }
474
479
480 public override object? Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture)
481 {
482 if ((CallingConvention & CallingConventions.VarArgs) == CallingConventions.VarArgs)
483 {
485 }
487 Signature signature = new Signature(m_methodHandle, m_parameterTypes, m_returnType, CallingConvention);
488 int num = signature.Arguments.Length;
489 int num2 = ((parameters != null) ? parameters.Length : 0);
490 if (num != num2)
491 {
493 }
494 bool wrapExceptions = (invokeAttr & BindingFlags.DoNotWrapExceptions) == 0;
495 StackAllocedArguments stackArgs = default(StackAllocedArguments);
496 Span<object> arguments = default(Span<object>);
497 if (num2 != 0)
498 {
499 arguments = CheckArguments(ref stackArgs, parameters, binder, invokeAttr, culture, signature);
500 }
501 object result = RuntimeMethodHandle.InvokeMethod(null, in arguments, signature, constructor: false, wrapExceptions);
502 for (int i = 0; i < arguments.Length; i++)
503 {
504 parameters[i] = arguments[i];
505 }
506 GC.KeepAlive(this);
507 return result;
508 }
509
510 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
511 {
512 return m_dynMethod.GetCustomAttributes(attributeType, inherit);
513 }
514
515 public override object[] GetCustomAttributes(bool inherit)
516 {
517 return m_dynMethod.GetCustomAttributes(inherit);
518 }
519
520 public override bool IsDefined(Type attributeType, bool inherit)
521 {
522 return m_dynMethod.IsDefined(attributeType, inherit);
523 }
524
525 public ParameterBuilder? DefineParameter(int position, ParameterAttributes attributes, string? parameterName)
526 {
527 if (position < 0 || position > m_parameterTypes.Length)
528 {
530 }
531 position--;
532 if (position >= 0)
533 {
535 array[position].SetName(parameterName);
536 array[position].SetAttributes(attributes);
537 }
538 return null;
539 }
540
542 {
543 if (m_DynamicILInfo == null)
544 {
545 CallingConventions callingConvention = CallingConvention;
546 Type returnType = ReturnType;
547 Type[] parameterTypes = m_parameterTypes;
548 byte[] signature = SignatureHelper.GetMethodSigHelper(null, callingConvention, returnType, null, null, parameterTypes, null, null).GetSignature(appendEndOfSig: true);
549 m_DynamicILInfo = new DynamicILInfo(this, signature);
550 }
551 return m_DynamicILInfo;
552 }
553
555 {
556 return GetILGenerator(64);
557 }
558
559 public ILGenerator GetILGenerator(int streamSize)
560 {
561 if (m_ilGenerator == null)
562 {
563 CallingConventions callingConvention = CallingConvention;
564 Type returnType = ReturnType;
565 Type[] parameterTypes = m_parameterTypes;
566 byte[] signature = SignatureHelper.GetMethodSigHelper(null, callingConvention, returnType, null, null, parameterTypes, null, null).GetSignature(appendEndOfSig: true);
567 m_ilGenerator = new DynamicILGenerator(this, signature, streamSize);
568 }
569 return m_ilGenerator;
570 }
571
573 {
574 return m_dynMethod;
575 }
576}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
static Delegate CreateDelegateNoSecurityCheck(Type type, object target, RuntimeMethodHandle method)
Definition Delegate.cs:288
static void KeepAlive(object? obj)
Definition GC.cs:180
Definition GC.cs:8
void StoreDynamicMethod(MethodInfo dynamicMethod)
static AssemblyBuilder InternalDefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access, ref StackCrawlMark stackMark, AssemblyLoadContext assemblyLoadContext, IEnumerable< CustomAttributeBuilder > unsafeAssemblyAttributes)
void GetCallableMethod(RuntimeModule module, DynamicMethod dm)
void GetCallableMethod(RuntimeModule module, DynamicMethod dm)
override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
override bool IsDefined(Type attributeType, bool inherit)
RTDynamicMethod(DynamicMethod owner, string name, MethodAttributes attributes, CallingConventions callingConvention)
override ICustomAttributeProvider ReturnTypeCustomAttributes
override object[] GetCustomAttributes(bool inherit)
override MethodImplAttributes GetMethodImplementationFlags()
override object[] GetCustomAttributes(Type attributeType, bool inherit)
DynamicMethod(string name, Type? returnType, Type[]? parameterTypes, Module m)
ParameterBuilder? DefineParameter(int position, ParameterAttributes attributes, string? parameterName)
override ICustomAttributeProvider ReturnTypeCustomAttributes
override Delegate CreateDelegate(Type delegateType)
override object[] GetCustomAttributes(Type attributeType, bool inherit)
DynamicMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes, Module m, bool skipVisibility)
override object[] GetCustomAttributes(bool inherit)
RuntimeMethodHandle GetMethodDescriptor()
DynamicMethod(string name, Type? returnType, Type[]? parameterTypes, Module m, bool skipVisibility)
override MethodAttributes Attributes
override MethodInfo GetBaseDefinition()
DynamicMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes, Type owner, bool skipVisibility)
DynamicMethod(string name, Type? returnType, Type[]? parameterTypes, Type owner)
static readonly object s_anonymouslyHostedDynamicMethodsModuleLock
override ParameterInfo[] GetParameters()
DynamicMethod(string name, Type? returnType, Type[]? parameterTypes)
DynamicMethod(string name, Type? returnType, Type[]? parameterTypes, Type owner, bool skipVisibility)
override bool IsDefined(Type attributeType, bool inherit)
override ParameterInfo ReturnParameter
static RuntimeModule GetDynamicMethodsModule()
override RuntimeMethodHandle MethodHandle
static void CheckConsistency(MethodAttributes attributes, CallingConventions callingConvention)
DynamicMethod(string name, Type? returnType, Type[]? parameterTypes, bool restrictedSkipVisibility)
override Delegate CreateDelegate(Type delegateType, object? target)
void Init(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] signature, Type owner, Module m, bool skipVisibility, bool transparentMethod)
override MethodImplAttributes GetMethodImplementationFlags()
override CallingConventions CallingConvention
static volatile InternalModuleBuilder s_anonymouslyHostedDynamicMethodsModule
override? object Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture)
ILGenerator GetILGenerator(int streamSize)
static SignatureHelper GetMethodSigHelper(Module? mod, Type? returnType, Type[]? parameterTypes)
virtual Type[] GetParameterTypes()
static void AppendParameters(ref ValueStringBuilder sbParamList, Type[] parameterTypes, CallingConventions callingConvention)
Span< object > CheckArguments(ref StackAllocedArguments stackArgs, ReadOnlySpan< object > parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
ModuleHandle ModuleHandle
Definition Module.cs:64
override bool ContainsGenericParameters
RuntimeModule GetRuntimeModule()
override bool IsGenericParameter
static void CompileMethod(RuntimeMethodHandleInternal method)
static string NotSupported_CallToVarArg
Definition SR.cs:1656
static string NotSupported_DynamicMethodFlags
Definition SR.cs:1670
static string Argument_InvalidTypeForDynamicMethod
Definition SR.cs:748
static string ArgumentOutOfRange_ParamSequence
Definition SR.cs:1088
static string Arg_InvalidTypeInRetType
Definition SR.cs:218
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string InvalidOperation_NotAllowedInDynamicMethod
Definition SR.cs:1480
static string Argument_MustBeRuntimeMethodInfo
Definition SR.cs:778
static string Arg_ParmCnt
Definition SR.cs:368
static string Arg_InvalidTypeInSignature
Definition SR.cs:220
static string InvalidOperation_BadEmptyMethodBody
Definition SR.cs:1400
Definition SR.cs:7
RuntimeType[] Arguments
Definition Signature.cs:29
bool IsInterface
Definition Type.cs:30
virtual bool IsAssignableFrom([NotNullWhen(true)] Type? c)
Definition Type.cs:1561
Type UnderlyingSystemType
Definition Type.cs:61
string FormatTypeName()
Definition Type.cs:1098
bool HasElementType
Definition Type.cs:143
RuntimeModule GetRuntimeModule()
static RuntimeMethodHandleInternal EmptyHandle
static object InvokeMethod(object target, in Span< object > arguments, Signature sig, bool constructor, bool wrapExceptions)
int Length
Definition Span.cs:70