/// <summary> /// 移除该静态事件的处理器。 /// </summary> /// <param name="delegate">事件处理器</param> public void RemoveEventHandler(Delegate @delegate) { if (@delegate != null && !_handler_type.IsInstanceOfType(@delegate)) { ThrowTargetException(nameof(@delegate), _handler_type); } if (_remove == default) { ThrowMissingMethodException("Event", EventInfo.DeclaringType, EventInfo, "remove"); } switch (_type) { case _type_static: Underlying.Call <Delegate>(@delegate, _remove); break; case _type_struct: case _type_class: ThrowInvalidOperationException("event", "static"); break; } }
/// <summary> /// 移除该实例事件的处理器。 /// </summary> /// <param name="reference">类型的实例引用</param> /// <param name="delegate">事件处理器</param> public void RemoveEventHandler(TypedReference reference, Delegate @delegate) { if (@delegate != null && !_handler_type.IsInstanceOfType(@delegate)) { ThrowTargetException(nameof(@delegate), _handler_type); } if (_remove == default) { ThrowMissingMethodException("Event", EventInfo.DeclaringType, EventInfo, "remove"); } switch (_type) { case _type_static: ThrowInvalidOperationException("event", "instance"); break; case _type_struct: if (!_declaring_type.IsSubclassOf(__reftype(reference))) { ThrowTargetException(nameof(reference), _declaring_type); } fixed(byte *ptr = &TypeHelper.RefValue <byte>(reference)) Underlying.Call <IntPtr, Delegate>((IntPtr)ptr, @delegate, _remove); break; case _type_class: var obj = TypeHelper.RefValue <object>(reference); if (!_declaring_type.IsInstanceOfType(obj)) { ThrowTargetException(nameof(reference), _declaring_type); } Underlying.Call <object, Delegate>(obj, @delegate, _remove); break; } }
/// <summary> /// 添加该实例事件的处理器。 /// </summary> /// <param name="obj">类型的实例</param> /// <param name="delegate">事件处理器</param> public void AddEventHandler(object obj, Delegate @delegate) { if (@delegate != null && !_handler_type.IsInstanceOfType(@delegate)) { ThrowTargetException(nameof(@delegate), _handler_type); } if (_add == default) { ThrowMissingMethodException("Event", EventInfo.DeclaringType, EventInfo, "add"); } switch (_type) { case _type_static: ThrowInvalidOperationException("event", "instance"); break; case _type_struct: if (!_declaring_type.IsInstanceOfType(obj)) { ThrowTargetException(nameof(obj), _declaring_type); } fixed(byte *ptr = &TypeHelper.Unbox <byte>(obj)) Underlying.Call <IntPtr, Delegate>((IntPtr)ptr, @delegate, _add); break; case _type_class: if (!_declaring_type.IsInstanceOfType(obj)) { ThrowTargetException(nameof(obj), _declaring_type); } Underlying.Call <object, Delegate>(obj, @delegate, _add); break; } }
internal static object CallConstructor(object obj, SerializationInfo serializationInfo) { var type = obj.GetType(); var constructor = type.GetConstructor( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, Type.DefaultBinder, new Type[] { typeof(SerializationInfo), typeof(StreamingContext) }, null); if (constructor is null) { throw new NotSupportedException("Missing deserialization constructor function."); } var pFunc = constructor.MethodHandle.GetFunctionPointer(); var streamingContext = new StreamingContext(StreamingContextStates.All); if (type.IsValueType) { unsafe { fixed(byte *ptr = &TypeHelper.Unbox <byte>(obj)) { Underlying.Call((IntPtr)ptr, serializationInfo, streamingContext, pFunc); } } } else { Underlying.Call(obj, serializationInfo, streamingContext, pFunc); } if (obj is IObjectReference objRef) { obj = objRef.GetRealObject(streamingContext); } return(obj); }