Joe Mamma Posted April 14, 2005 Posted April 14, 2005 Works for most components, a similar approach can be used on third party controls where this doesnt work. Typical Usage (assumes project appropriate references): ArrayList eventData = new ArrayList(); EventDescriptorCollection events = TypeDescriptor.GetEvents(myComponent); foreach (EventDescriptor myEvent in events) { // Unwire the events . . . EventDatum ed = EventDatum.Create(myComponent, myEvent); if (ed == null) continue; EventData.Add(ed); ed.Unwire(myComponent); } //Do something with component here . . . // now rewire . . . foreach(EventDatum ed in EventData) if (ed != null) ed.Wire(myComponent); EventData.Clear(); EventDatum listing (assumes project appropriate references) public class EventDatum { private EventDescriptor _eventDesc; private Delegate _event; private static MethodInfo GetEventsMethod(Type objType) { MethodInfo mi = objType.GetMethod("get_Events",All); if ((mi ==null)& (objType.BaseType!=null)) mi = GetEventsMethod(objType.BaseType); return mi; } private static EventHandlerList GetEvents(object obj) { MethodInfo mi = GetEventsMethod(obj.GetType()); if (mi == null) return null; return (EventHandlerList) mi.Invoke(obj, new object[]{}); } private static FieldInfo GetEventIDField(Type objType, string eventName) { FieldInfo fi = objType.GetField("Event"+eventName,All); if ((fi ==null)& (objType.BaseType!=null)) fi = GetEventIDField(objType.BaseType,eventName); return fi; } private static object GetEventID(object obj, string eventName) { FieldInfo fi = GetEventIDField(obj.GetType(), eventName); if (fi ==null) return null; return fi.GetValue(obj); } private static BindingFlags All { get { return BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance| BindingFlags.IgnoreCase| BindingFlags.Static; } } internal EventDatum(EventDescriptor desc, Delegate aEvent) { _eventDesc = desc; _event = aEvent; } public static EventDatum Create(object obj, EventDescriptor desc) { EventHandlerList list = GetEvents(obj); if (list==null) return null; object key = GetEventID(obj, desc.Name); if (key==null) return null; Delegate evnt = list[key]; if (evnt == null) return null; return new EventDatum(desc, evnt); } public void Wire(object obj) { _eventDesc.AddEventHandler(obj, _event); } public void Unwire(object obj) { _eventDesc.RemoveEventHandler(obj, _event); } } Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.