private void MapMembers() { // we want to loop through all the attributes on all the members of the source var targetMembers = Type.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (var member in targetMembers) { var attr = member.GetCustomAttribute<SpliceAttribute>(false); if (attr != null) { var mapping = new MemberMapping(Type, member, attr); Members.Add(member, mapping); } } // we want to loop through all the attributes on all the methods of the source var targeMethods = Type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); foreach (var method in targeMethods) { var attrs = method.GetCustomAttributes<SpliceEventAttribute>(false); foreach (var attr in attrs) { var mapping = new MethodMapping(Type, method, attr); if (!Methods.ContainsKey(method)) { Methods.Add(method, new List<MethodMapping>()); } Methods[method].Add(mapping); } } }
protected Delegate CreateEventHandler(object target, View view, MethodMapping methodMapping, out EventInfo evnt) { Delegate del = null; evnt = null; if (view != null) { evnt = view.GetType().GetEvent(methodMapping.Attribute.EventName); if (evnt != null) { try { del = CreateDelegate(target, methodMapping, evnt); } catch (Exception ex) { Geneticist.HandleError( ex, "Error creating delegate from '{0}' for event '{1}'.", methodMapping.Method.Name, methodMapping.Attribute.EventName); } } else { Geneticist.HandleError( "Unable to find event '{0}' for method '{1}'.", methodMapping.Attribute.EventName, methodMapping.Method.Name); } } return del; }
public virtual void Sever(object target, object source, View view, int viewId, Context context, MethodMapping methodMapping) { // the view may have been disposed, // thus we don't need to detach if (view != null) { DetachDelegate(target, view, methodMapping); } }
protected static Delegate CreateDelegate(object target, MethodMapping methodMapping, EventInfo evnt) { var match = MethodMapping.CompareMethods(evnt.EventHandlerType.GetMethod("Invoke"), methodMapping.Method); if (match == MethodMapping.MethodMatch.SimilarParameters) { return Delegate.CreateDelegate(evnt.EventHandlerType, target, methodMapping.Method); } //else if (match == MethodMapping.MethodMatch.SenderParameter) //{ // return null; //} //else if (match == MethodMapping.MethodMatch.NoParameters) //{ // return null; //} return null; }
protected bool AttachDelegate(object target, View view, MethodMapping methodMapping) { // get the handler EventInfo evnt; var del = CreateEventHandler(target, view, methodMapping, out evnt); // attach it var attached = false; try { evnt.AddEventHandler(view, del); attached = true; } catch (Exception ex) { Geneticist.HandleError( ex, "Unable to attach delegate '{0}' to event '{1}'.", methodMapping.Method.Name, methodMapping.Attribute.EventName); } return attached; }
public virtual bool Splice(object target, object source, View view, int viewId, Context context, MethodMapping methodMapping) { return AttachDelegate(target, view, methodMapping); }