/// <summary> /// Verifies that two events are equivalent. /// </summary> /// <param name="actual">The actual event information.</param> /// <param name="expected">The expected event information.</param> /// <param name="isExplicit">A value indicating whether the actual event is implemented explicitly.</param> public static void AreEquivalent(EventInfo actual, EventInfo expected, bool isExplicit) { Assert.That(actual, Is.Not.Null); Assert.That(expected, Is.Not.Null); // Check member properties. AreMembersEquivalent(actual, expected); // Check event properties. Assert.That(actual.Attributes, Is.EqualTo(expected.Attributes)); Assert.That(actual.IsMulticast, Is.EqualTo(expected.IsMulticast)); Assert.That(actual.IsSpecialName, Is.EqualTo(expected.IsSpecialName)); // Check event handler type. AreTypesEquivalent(actual.EventHandlerType, expected.EventHandlerType); // Check event methods. InternalAreEquivalent(actual.GetAddMethod(), expected.GetAddMethod(), isExplicit); InternalAreEquivalent(actual.GetRemoveMethod(), expected.GetRemoveMethod(), isExplicit); InternalAreEquivalent(actual.GetRaiseMethod(), expected.GetRaiseMethod(), isExplicit); }
public static bool RaiseEvent(object getInstance, PlTerm memberSpec, PlTerm[] paramIn, PlTerm valueOut, EventInfo evi, Type c) { if (evi == null) { return Warn("Cant find event {0} on {1}", memberSpec, c); } ParameterInfo[] paramInfos = GetParmeters(evi); MethodInfo mi = evi.GetRaiseMethod(); string fn = evi.Name; if (mi == null) { FieldInfo fi = c.GetField(fn, BindingFlagsALLNC); if (fi != null) { Delegate del = (Delegate) fi.GetValue(getInstance); if (del != null) { Action postCallHook; var ret = valueOut.FromObject((del.DynamicInvoke( PlListToCastedArray(paramIn, paramInfos, out postCallHook)))); CommitPostCall(postCallHook); return ret; } } string fn1 = fn.Substring(1); int len = fn.Length; foreach (FieldInfo info in c.GetFields(BindingFlagsALL)) { if (info.Name.EndsWith(fn1)) { if (info.Name.Length - len < 3) { Delegate del = (Delegate) info.GetValue(info.IsStatic ? null : getInstance); if (del != null) { Action postCallHook; var ret = valueOut.FromObject((del.DynamicInvoke( PlListToCastedArray(paramIn, paramInfos, out postCallHook)))); CommitPostCall(postCallHook); return ret; } } } } } if (mi == null) { Type eviEventHandlerType = evi.EventHandlerType; if (eviEventHandlerType != null) mi = eviEventHandlerType.GetMethod("Invoke"); } if (mi == null) { Warn("Cant find event raising for {0} on {1}", evi, c); return false; } Action postCallHook0; object[] value = PlListToCastedArray(paramIn, mi.GetParameters(), out postCallHook0); object target = mi.IsStatic ? null : getInstance; return valueOut.FromObject(InvokeCaught(mi, target, value, postCallHook0) ?? VoidOrNull(mi)); }
private static ParameterInfo[] GetParmeters(EventInfo ei) { ParameterInfo[] parme = null; var rm = ei.GetRaiseMethod(); var erm = ei.EventHandlerType; if (rm == null && erm != null) { rm = erm.GetMethod("Invoke"); } if (rm != null) { parme = rm.GetParameters(); } return parme; }
public override System.Reflection.MethodInfo GetRaiseMethod(bool nonPublic) { return(_innerEventInfo.GetRaiseMethod(nonPublic)); }
public static MethodInfo?GetRaiseMethod(this EventInfo eventInfo, bool nonPublic) { ArgumentNullException.ThrowIfNull(eventInfo); return(eventInfo.GetRaiseMethod(nonPublic)); }
/// <summary> /// Defines an event based on the specified event. /// </summary> /// <param name="typeBuilder">The type builder.</param> /// <param name="eventInfo">The event information.</param> /// <param name="isExplicit">A value indicating whether the specified event should be implemented explicitly.</param> /// <param name="methodBuilderFactory">The method builder factory function.</param> /// <returns>The event builder.</returns> public static void DefineEvent(this TypeBuilder typeBuilder, EventInfo eventInfo, bool isExplicit, Func<MethodInfo, bool, MethodBuilder> methodBuilderFactory) { if (typeBuilder == null) throw new ArgumentNullException("typeBuilder"); if (eventInfo == null) throw new ArgumentNullException("eventInfo"); if (methodBuilderFactory == null) throw new ArgumentNullException("methodBuilderFactory"); // Define event. var eventName = isExplicit ? eventInfo.GetFullName() : eventInfo.Name; var eventBuilder = typeBuilder.DefineEvent( eventName, eventInfo.Attributes, eventInfo.EventHandlerType); // Build event add method. var addMethodInfo = eventInfo.GetAddMethod(); var addMethodBuilder = methodBuilderFactory(addMethodInfo, isExplicit); eventBuilder.SetAddOnMethod(addMethodBuilder); // Build event remove method. var removeMethodInfo = eventInfo.GetRemoveMethod(); var removeMethodBuilder = methodBuilderFactory(removeMethodInfo, isExplicit); eventBuilder.SetRemoveOnMethod(removeMethodBuilder); // Build event raise method. var raiseMethodInfo = eventInfo.GetRaiseMethod(); if (raiseMethodInfo != null) { var methodBuilder = methodBuilderFactory(raiseMethodInfo, isExplicit); eventBuilder.SetRaiseMethod(methodBuilder); } }
public static MethodInfo?GetRaiseMethod(this EventInfo eventInfo) { ArgumentNullException.ThrowIfNull(eventInfo, nameof(eventInfo)); return(eventInfo.GetRaiseMethod()); }
/// <summary> /// Defines an event based on the specified event. /// </summary> /// <param name="typeBuilder">The type builder.</param> /// <param name="eventInfo">The event information.</param> /// <param name="isExplicit">A value indicating whether the specified event should be implemented explicitly.</param> /// <param name="methodBuilderFactory">The method builder factory function.</param> /// <returns>The event builder.</returns> public static void DefineEvent(this TypeBuilder typeBuilder, EventInfo eventInfo, bool isExplicit, Func<MethodInfo, bool, MethodBuilder> methodBuilderFactory) { if (typeBuilder == null) throw new ArgumentNullException("typeBuilder"); if (eventInfo == null) throw new ArgumentNullException("eventInfo"); if (methodBuilderFactory == null) throw new ArgumentNullException("methodBuilderFactory"); // Define event. var eventName = isExplicit ? eventInfo.GetFullName() : eventInfo.Name; var eventBuilder = typeBuilder.DefineEvent( eventName, eventInfo.Attributes, eventInfo.EventHandlerType); // Build event add method. var addMethodInfo = eventInfo.GetAddMethod(); var addMethodBuilder = methodBuilderFactory(addMethodInfo, isExplicit); eventBuilder.SetAddOnMethod(addMethodBuilder); // Build event remove method. var removeMethodInfo = eventInfo.GetRemoveMethod(true); var removeMethodBuilder = methodBuilderFactory(removeMethodInfo, isExplicit); eventBuilder.SetRemoveOnMethod(removeMethodBuilder); // Build event raise method. var raiseMethodInfo = eventInfo.GetRaiseMethod(true); if (raiseMethodInfo != null) { var methodBuilder = methodBuilderFactory(raiseMethodInfo, isExplicit); eventBuilder.SetRaiseMethod(methodBuilder); } // Build event other methods. var otherMethodInfos = eventInfo.GetOtherMethods(true); // Mono returns null in case no other methods are defined. if (otherMethodInfos != null) { foreach (var otherMethodInfo in otherMethodInfos) { var methodBuilder = methodBuilderFactory(otherMethodInfo, isExplicit); eventBuilder.AddOtherMethod(methodBuilder); } } }
public static MethodInfo?GetRaiseMethod(this EventInfo eventInfo, bool nonPublic) { Requires.NotNull(eventInfo, nameof(eventInfo)); return(eventInfo.GetRaiseMethod(nonPublic)); }
private void MapEvent(EventInfo evt, EventDefinition event_definition) { if (_options.EraseMember(evt)) return; var type_definition = event_definition.DeclaringType; var add = evt.GetAddMethod(nonPublic: true); if (add != null) { event_definition.AddMethod = type_definition.Methods.Single(m => m.Name == add.Name); event_definition.AddMethod.IsAddOn = true; } var remove = evt.GetRemoveMethod(nonPublic: true); if (remove != null) { event_definition.RemoveMethod = type_definition.Methods.Single(m => m.Name == remove.Name); event_definition.RemoveMethod.IsRemoveOn = true; } var raise = evt.GetRaiseMethod(nonPublic: true); if (raise != null) { event_definition.InvokeMethod = type_definition.Methods.Single(m => m.Name == raise.Name); event_definition.InvokeMethod.IsFire = true; } MapCustomAttributes(evt, event_definition); }
private void BuildEvent(EventInfo eventInfo) { EventBuilder eventBuilder = m_typeBuilder.DefineEvent( eventInfo.Name, eventInfo.Attributes, eventInfo.EventHandlerType); MethodInfo addMethod = eventInfo.GetAddMethod(); if (addMethod != null) { MethodBuilder addMethodBuilder = m_interfaceMethodDict[addMethod]; eventBuilder.SetAddOnMethod(addMethodBuilder); } MethodInfo removeMethod = eventInfo.GetRemoveMethod(); if (removeMethod != null) { MethodBuilder removeMethodBuilder = m_interfaceMethodDict[removeMethod]; eventBuilder.SetRemoveOnMethod(removeMethodBuilder); } MethodInfo raiseMethod = eventInfo.GetRaiseMethod(); if (raiseMethod != null) { MethodBuilder raiseMethodBuilder = m_interfaceMethodDict[raiseMethod]; eventBuilder.SetRaiseMethod(raiseMethodBuilder); } }
static EventInfo GetBaseEventDefinition (EventInfo evt) { MethodInfo method = evt.GetAddMethod (true); if (method == null || !method.IsVirtual) method = evt.GetRaiseMethod (true); if (method == null || !method.IsVirtual) method = evt.GetRemoveMethod (true); if (method == null || !method.IsVirtual) return null; MethodInfo baseMethod = method.GetBaseMethod (); if (baseMethod != null && baseMethod != method) { BindingFlags flags = method.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic; flags |= method.IsStatic ? BindingFlags.Static : BindingFlags.Instance; return baseMethod.DeclaringType.GetEvent (evt.Name, flags); } return null; }