Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Delegate.cs
Go to the documentation of this file.
6
7namespace System;
8
10[ComVisible(true)]
11public abstract class Delegate : ICloneable, ISerializable
12{
13 internal object _target;
14
15 internal object _methodBase;
16
18
20
21 public object? Target => GetTarget();
22
24
25 [RequiresUnreferencedCode("The target method might be removed")]
26 protected Delegate(object target, string method)
27 {
28 if (target == null)
29 {
30 throw new ArgumentNullException("target");
31 }
32 if (method == null)
33 {
34 throw new ArgumentNullException("method");
35 }
36 if (!BindToMethodName(target, (RuntimeType)target.GetType(), method, (DelegateBindingFlags)10))
37 {
39 }
40 }
41
43 {
44 if (target == null)
45 {
46 throw new ArgumentNullException("target");
47 }
49 {
50 throw new ArgumentException(SR.Arg_UnboundGenParam, "target");
51 }
52 if (method == null)
53 {
54 throw new ArgumentNullException("method");
55 }
56 if (!(target is RuntimeType methodType))
57 {
59 }
61 }
62
68
69 public override bool Equals([NotNullWhen(true)] object? obj)
70 {
71 if (obj == null || !InternalEqualTypes(this, obj))
72 {
73 return false;
74 }
76 if (_target == @delegate._target && _methodPtr == @delegate._methodPtr && _methodPtrAux == @delegate._methodPtrAux)
77 {
78 return true;
79 }
81 {
82 if (@delegate._methodPtrAux != IntPtr.Zero)
83 {
84 return false;
85 }
86 if (_target != @delegate._target)
87 {
88 return false;
89 }
90 }
91 else
92 {
93 if (@delegate._methodPtrAux == IntPtr.Zero)
94 {
95 return false;
96 }
97 if (_methodPtrAux == @delegate._methodPtrAux)
98 {
99 return true;
100 }
101 }
102 if (_methodBase == null || @delegate._methodBase == null || !(_methodBase is MethodInfo) || !(@delegate._methodBase is MethodInfo))
103 {
105 }
106 return _methodBase.Equals(@delegate._methodBase);
107 }
108
109 public override int GetHashCode()
110 {
112 {
113 return ((_target != null) ? (RuntimeHelpers.GetHashCode(_target) * 33) : 0) + GetType().GetHashCode();
114 }
115 return GetType().GetHashCode();
116 }
117
118 protected virtual MethodInfo GetMethodImpl()
119 {
120 if (_methodBase == null || !(_methodBase is MethodInfo))
121 {
125 {
127 {
128 Type type = _target.GetType();
130 while (type != null)
131 {
132 if (type.IsGenericType && type.GetGenericTypeDefinition() == genericTypeDefinition)
133 {
135 break;
136 }
138 }
139 }
140 else
141 {
142 MethodInfo method = GetType().GetMethod("Invoke");
144 }
145 }
147 }
148 return (MethodInfo)_methodBase;
149 }
150
151 [RequiresUnreferencedCode("The target method might be removed")]
152 public static Delegate? CreateDelegate(Type type, object target, string method, bool ignoreCase, bool throwOnBindFailure)
153 {
154 if (type == null)
155 {
156 throw new ArgumentNullException("type");
157 }
158 if (target == null)
159 {
160 throw new ArgumentNullException("target");
161 }
162 if (method == null)
163 {
164 throw new ArgumentNullException("method");
165 }
167 {
169 }
170 if (!runtimeType.IsDelegate())
171 {
172 throw new ArgumentException(SR.Arg_MustBeDelegate, "type");
173 }
175 if (!@delegate.BindToMethodName(target, (RuntimeType)target.GetType(), method, (DelegateBindingFlags)26 | (ignoreCase ? DelegateBindingFlags.CaselessMatching : ((DelegateBindingFlags)0))))
176 {
178 {
180 }
181 return null;
182 }
183 return @delegate;
184 }
185
187 {
188 if (type == null)
189 {
190 throw new ArgumentNullException("type");
191 }
192 if (target == null)
193 {
194 throw new ArgumentNullException("target");
195 }
196 if (target.ContainsGenericParameters)
197 {
198 throw new ArgumentException(SR.Arg_UnboundGenParam, "target");
199 }
200 if (method == null)
201 {
202 throw new ArgumentNullException("method");
203 }
205 {
207 }
208 if (!(target is RuntimeType methodType))
209 {
211 }
212 if (!runtimeType.IsDelegate())
213 {
214 throw new ArgumentException(SR.Arg_MustBeDelegate, "type");
215 }
217 if (!@delegate.BindToMethodName(null, methodType, method, (DelegateBindingFlags)5 | (ignoreCase ? DelegateBindingFlags.CaselessMatching : ((DelegateBindingFlags)0))))
218 {
220 {
222 }
223 return null;
224 }
225 return @delegate;
226 }
227
229 {
230 if (type == null)
231 {
232 throw new ArgumentNullException("type");
233 }
234 if (method == null)
235 {
236 throw new ArgumentNullException("method");
237 }
239 {
241 }
243 {
245 }
246 if (!runtimeType.IsDelegate())
247 {
248 throw new ArgumentException(SR.Arg_MustBeDelegate, "type");
249 }
251 if ((object)@delegate == null && throwOnBindFailure)
252 {
254 }
255 return @delegate;
256 }
257
259 {
260 if (type == null)
261 {
262 throw new ArgumentNullException("type");
263 }
264 if (method == null)
265 {
266 throw new ArgumentNullException("method");
267 }
269 {
271 }
273 {
275 }
276 if (!runtimeType.IsDelegate())
277 {
278 throw new ArgumentException(SR.Arg_MustBeDelegate, "type");
279 }
281 if ((object)@delegate == null && throwOnBindFailure)
282 {
284 }
285 return @delegate;
286 }
287
289 {
290 if (type == null)
291 {
292 throw new ArgumentNullException("type");
293 }
294 if (method.IsNullHandle())
295 {
296 throw new ArgumentNullException("method");
297 }
299 {
301 }
302 if (!runtimeType.IsDelegate())
303 {
304 throw new ArgumentException(SR.Arg_MustBeDelegate, "type");
305 }
307 if (!@delegate.BindToMethodInfo(target, method.GetMethodInfo(), RuntimeMethodHandle.GetDeclaringType(method.GetMethodInfo()), DelegateBindingFlags.RelaxedSignature))
308 {
310 }
311 return @delegate;
312 }
313
315 {
317 if (@delegate.BindToMethodInfo(firstArgument, rtMethod, rtMethod.GetDeclaringTypeInternal(), flags))
318 {
319 return @delegate;
320 }
321 return null;
322 }
323
324 [MethodImpl(MethodImplOptions.InternalCall)]
326
327 [MethodImpl(MethodImplOptions.InternalCall)]
329
330 [MethodImpl(MethodImplOptions.InternalCall)]
332
333 [MethodImpl(MethodImplOptions.InternalCall)]
335
336 [MethodImpl(MethodImplOptions.InternalCall)]
337 internal static extern bool InternalEqualTypes(object a, object b);
338
339 [MethodImpl(MethodImplOptions.InternalCall)]
340 private extern void DelegateConstruct(object target, IntPtr slot);
341
342 [MethodImpl(MethodImplOptions.InternalCall)]
343 internal extern IntPtr GetMulticastInvoke();
344
345 [MethodImpl(MethodImplOptions.InternalCall)]
346 internal extern IntPtr GetInvokeMethod();
347
348 [MethodImpl(MethodImplOptions.InternalCall)]
350
351 [MethodImpl(MethodImplOptions.InternalCall)]
352 internal static extern bool InternalEqualMethodHandles(Delegate left, Delegate right);
353
354 [MethodImpl(MethodImplOptions.InternalCall)]
355 internal extern IntPtr AdjustTarget(object target, IntPtr methodPtr);
356
357 [MethodImpl(MethodImplOptions.InternalCall)]
359
360 internal virtual object GetTarget()
361 {
362 if (!(_methodPtrAux == IntPtr.Zero))
363 {
364 return null;
365 }
366 return _target;
367 }
368
369 [MethodImpl(MethodImplOptions.InternalCall)]
370 internal static extern bool CompareUnmanagedFunctionPtrs(Delegate d1, Delegate d2);
371
372 public virtual object Clone()
373 {
374 return MemberwiseClone();
375 }
376
377 [return: NotNullIfNotNull("a")]
378 [return: NotNullIfNotNull("b")]
379 public static Delegate? Combine(Delegate? a, Delegate? b)
380 {
381 if ((object)a == null)
382 {
383 return b;
384 }
385 return a.CombineImpl(b);
386 }
387
389 {
390 if (delegates == null || delegates.Length == 0)
391 {
392 return null;
393 }
395 for (int i = 1; i < delegates.Length; i++)
396 {
398 }
399 return @delegate;
400 }
401
406
411
412 [RequiresUnreferencedCode("The target method might be removed")]
413 public static Delegate CreateDelegate(Type type, object target, string method)
414 {
415 return CreateDelegate(type, target, method, ignoreCase: false, throwOnBindFailure: true);
416 }
417
418 [RequiresUnreferencedCode("The target method might be removed")]
419 public static Delegate CreateDelegate(Type type, object target, string method, bool ignoreCase)
420 {
421 return CreateDelegate(type, target, method, ignoreCase, throwOnBindFailure: true);
422 }
423
428
433
434 protected virtual Delegate CombineImpl(Delegate? d)
435 {
437 }
438
439 protected virtual Delegate? RemoveImpl(Delegate d)
440 {
441 if (!d.Equals(this))
442 {
443 return this;
444 }
445 return null;
446 }
447
448 public virtual Delegate[] GetInvocationList()
449 {
450 return new Delegate[1] { this };
451 }
452
453 public object? DynamicInvoke(params object?[]? args)
454 {
455 return DynamicInvokeImpl(args);
456 }
457
459 {
461 }
462
464 {
465 if ((object)source == null)
466 {
467 return null;
468 }
469 if ((object)value == null)
470 {
471 return source;
472 }
474 {
476 }
477 return source.RemoveImpl(value);
478 }
479
481 {
483 do
484 {
485 @delegate = source;
487 }
488 while (@delegate != source);
489 return @delegate;
490 }
491
492 [MethodImpl(MethodImplOptions.AggressiveInlining)]
493 public static bool operator ==(Delegate? d1, Delegate? d2)
494 {
495 if ((object)d2 == null)
496 {
497 if ((object)d1 != null)
498 {
499 return false;
500 }
501 return true;
502 }
503 if ((object)d2 != d1)
504 {
505 return d2.Equals(d1);
506 }
507 return true;
508 }
509
510 [MethodImpl(MethodImplOptions.AggressiveInlining)]
511 public static bool operator !=(Delegate? d1, Delegate? d2)
512 {
513 if ((object)d2 == null)
514 {
515 if ((object)d1 != null)
516 {
517 return true;
518 }
519 return false;
520 }
521 if ((object)d2 != d1)
522 {
523 return !d2.Equals(d1);
524 }
525 return false;
526 }
527}
IntPtr _methodPtr
Definition Delegate.cs:17
object _target
Definition Delegate.cs:13
void DelegateConstruct(object target, IntPtr slot)
bool BindToMethodInfo(object target, IRuntimeMethodInfo method, RuntimeType methodType, DelegateBindingFlags flags)
static ? Delegate CreateDelegate(Type type, object? firstArgument, MethodInfo method, bool throwOnBindFailure)
Definition Delegate.cs:258
virtual Delegate[] GetInvocationList()
Definition Delegate.cs:448
static Delegate CreateDelegate(Type type, object? firstArgument, MethodInfo method)
Definition Delegate.cs:402
static bool InternalEqualMethodHandles(Delegate left, Delegate right)
virtual ? Delegate RemoveImpl(Delegate d)
Definition Delegate.cs:439
static ? Delegate CreateDelegate(Type type, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type target, string method, bool ignoreCase, bool throwOnBindFailure)
Definition Delegate.cs:186
static ? Delegate RemoveAll(Delegate? source, Delegate? value)
Definition Delegate.cs:480
object? DynamicInvoke(params object?[]? args)
Definition Delegate.cs:453
bool BindToMethodName(object target, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] RuntimeType methodType, string method, DelegateBindingFlags flags)
static ? Delegate Combine(params Delegate?[]? delegates)
Definition Delegate.cs:388
IRuntimeMethodInfo FindMethodHandle()
static Delegate CreateDelegate(Type type, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type target, string method)
Definition Delegate.cs:424
IntPtr _methodPtrAux
Definition Delegate.cs:19
IntPtr GetInvokeMethod()
static ? Delegate CreateDelegate(Type type, object target, string method, bool ignoreCase, bool throwOnBindFailure)
Definition Delegate.cs:152
virtual object Clone()
Definition Delegate.cs:372
IntPtr AdjustTarget(object target, IntPtr methodPtr)
static Delegate CreateDelegate(Type type, object target, string method, bool ignoreCase)
Definition Delegate.cs:419
virtual void GetObjectData(SerializationInfo info, StreamingContext context)
Definition Delegate.cs:458
virtual Delegate CombineImpl(Delegate? d)
Definition Delegate.cs:434
static Delegate CreateDelegateInternal(RuntimeType rtType, RuntimeMethodInfo rtMethod, object firstArgument, DelegateBindingFlags flags)
Definition Delegate.cs:314
static MulticastDelegate InternalAllocLike(Delegate d)
IntPtr GetCallStub(IntPtr methodPtr)
static MulticastDelegate InternalAlloc(RuntimeType type)
virtual MethodInfo GetMethodImpl()
Definition Delegate.cs:118
static Delegate CreateDelegate(Type type, MethodInfo method)
Definition Delegate.cs:407
object _methodBase
Definition Delegate.cs:15
static Delegate CreateDelegateNoSecurityCheck(Type type, object target, RuntimeMethodHandle method)
Definition Delegate.cs:288
MethodInfo Method
Definition Delegate.cs:23
virtual ? object DynamicInvokeImpl(object?[]? args)
Definition Delegate.cs:63
static bool operator==(Delegate? d1, Delegate? d2)
Definition Delegate.cs:493
static Delegate CreateDelegate(Type type, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type target, string method, bool ignoreCase)
Definition Delegate.cs:429
Delegate([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type target, string method)
Definition Delegate.cs:42
Delegate(object target, string method)
Definition Delegate.cs:26
virtual object GetTarget()
Definition Delegate.cs:360
override int GetHashCode()
Definition Delegate.cs:109
static bool InternalEqualTypes(object a, object b)
static Delegate CreateDelegate(Type type, object target, string method)
Definition Delegate.cs:413
static bool operator!=(Delegate? d1, Delegate? d2)
Definition Delegate.cs:511
static bool CompareUnmanagedFunctionPtrs(Delegate d1, Delegate d2)
IntPtr GetMulticastInvoke()
object? Target
Definition Delegate.cs:21
static ? Delegate CreateDelegate(Type type, MethodInfo method, bool throwOnBindFailure)
Definition Delegate.cs:228
static ? Delegate Remove(Delegate? source, Delegate? value)
Definition Delegate.cs:463
override bool Equals([NotNullWhen(true)] object? obj)
Definition Delegate.cs:69
static ? Delegate Combine(Delegate? a, Delegate? b)
Definition Delegate.cs:379
ParameterInfo[] GetParameters()
override Type BaseType
static MethodBase GetMethodBase(RuntimeModule scope, int typeMetadataToken)
static string Arg_UnboundGenParam
Definition SR.cs:444
static string Arg_MustBeDelegate
Definition SR.cs:268
static string Arg_DlgtTypeMis
Definition SR.cs:124
static string Argument_MustBeRuntimeMethodInfo
Definition SR.cs:778
static string Arg_DlgtTargMeth
Definition SR.cs:122
static string Multicast_Combine
Definition SR.cs:1642
static string Argument_MustBeRuntimeType
Definition SR.cs:782
Definition SR.cs:7
virtual bool ContainsGenericParameters
Definition Type.cs:336
virtual Type GetGenericTypeDefinition()
Definition Type.cs:495
static readonly IntPtr Zero
Definition IntPtr.cs:18
static RuntimeType GetDeclaringType(RuntimeMethodHandleInternal method)
static MethodAttributes GetAttributes(RuntimeMethodHandleInternal method)
static bool HasInstantiation(RuntimeType type)
static bool IsGenericTypeDefinition(RuntimeType type)