public MethodInfo[] getAddRemoveMethodsForEvent(Type targetType, String eventName) { ParamChecker.AssertParamNotNull(targetType, "targetType"); ParamChecker.AssertParamNotNull(eventName, "eventName"); MethodInfo[] addRemoveMethods; EventKeyItem keyItem = new EventKeyItem(targetType, eventName); lock (typeToAddRemoveMethodsMapOld) { addRemoveMethods = DictionaryExtension.ValueOrDefault(typeToAddRemoveMethodsMapOld, keyItem); if (addRemoveMethods != null) { return(addRemoveMethods); } } MethodInfo addMethod = targetType.GetMethod("add_" + eventName); MethodInfo removeMethod = targetType.GetMethod("remove_" + eventName); if (addMethod == null || removeMethod == null) { throw new ExtendableException("No autogenerated event methods for event field '" + eventName + "' found on type " + targetType.Name + ". Looked for a member like 'public event " + typeof(PropertyChangedEventHandler).Name + " " + eventName + ";"); } addRemoveMethods = new MethodInfo[] { addMethod, removeMethod }; lock (typeToAddRemoveMethodsMapOld) { if (!typeToAddRemoveMethodsMapOld.ContainsKey(keyItem)) { typeToAddRemoveMethodsMapOld.Add(keyItem, addRemoveMethods); } } return(addRemoveMethods); }
public override bool Equals(Object obj) { if (Object.ReferenceEquals(obj, this)) { return(true); } if (!(obj is EventKeyItem)) { return(false); } EventKeyItem other = (EventKeyItem)obj; return(Object.Equals(type, other.type) && Object.Equals(eventName, other.eventName)); }
protected MethodInfo[] getAddRemoveMethodsForEvent(Type targetType) { ParamChecker.AssertParamNotNull(targetType, "targetType"); MethodInfo[] addRemoveMethods; EventKeyItem keyItem = new EventKeyItem(targetType, null); lock (typeToAddRemoveMethodsMapOld) { addRemoveMethods = DictionaryExtension.ValueOrDefault(typeToAddRemoveMethodsMapOld, keyItem); if (addRemoveMethods != null) { return(addRemoveMethods); } } MethodInfo[] methods = targetType.GetMethods(); MethodInfo addMethod = null, removeMethod = null; foreach (MethodInfo method in methods) { if (!method.IsSpecialName) { // Look for special methods autogenerated by the 'event' keyword continue; } ParameterInfo[] paramInfos = method.GetParameters(); if (paramInfos.Length != 1) { // The autogenerated methods by the 'event' keyword always have exactly 1 argument continue; } String methodName = method.Name; if (methodName.StartsWith("add_")) { if (addMethod != null) { throw new ExtendableException("Autogenerated event methods not uniquely resolvable. Maybe there are more than exactly 1 members like 'public event <EventType> <EventName>;' on type " + targetType.FullName + "?"); } addMethod = method; } else if (methodName.StartsWith("remove_")) { if (removeMethod != null) { throw new ExtendableException("Autogenerated event methods not uniquely resolvable. Maybe there are more than exactly 1 members like 'public event <EventType> <EventName>;' on type " + targetType.FullName + "?"); } removeMethod = method; } } if (addMethod == null || removeMethod == null) { throw new ExtendableException("No autogenerated event methods found. Looked for a member like 'public event <EventType> <EventName>;' on type " + targetType.FullName); } addRemoveMethods = new MethodInfo[] { addMethod, removeMethod }; lock (typeToAddRemoveMethodsMapOld) { if (!typeToAddRemoveMethodsMapOld.ContainsKey(keyItem)) { typeToAddRemoveMethodsMapOld.Add(keyItem, addRemoveMethods); } } return(addRemoveMethods); }