// HOW-TO: Connect to an event source (IConnectionPoint) public static void How_to_connect_to_an_event_source(IEngine engine) { // COM objects implement IConnectionPoint interface to make events // they support available to clients. Working directly with this interface // is possible but rather tedious. To facilitate working with events, // the .NET Framework generates a set of helper classes when importing // types information from a type library. // For example, to subscribe to ICollectionEvents from an object, you use // ICollectionEvents_Event helper class. Casting an object to this class // will automatically check if the object implements IConnectionPoint interface // and connect to ICollectionEvents if found. If the connection point is not available, // the cast will result in a null: trace("Cast the object to the helper class for the required event source."); ICollectionEvents_Event collectionEvents = engine as ICollectionEvents_Event; if (collectionEvents != null) { // IEngine does not support this kind of events, but if it did, we could // assign a method to handle the events: trace("Assign a method to be called when a particular event occurs."); // collectionEvents.OnCollectionChanged += // new ICollectionEvents_OnCollectionChangedEventHandler( ... ); } }
public void Dispose() { collectionEvents.OnCollectionChanged -= new ICollectionEvents_OnCollectionChangedEventHandler( collectionEvents_OnCollectionChanged ); collectionEvents = null; }
public SampleCollectionEventsSink( object source, string _name ) { // Check that the source object is a ICollectionEvents source (most collections are). // Using the helper class generated by the framework which maps event source methods to delegates collectionEvents = source as ICollectionEvents_Event; assert( collectionEvents != null ); name = _name; collectionEvents.OnCollectionChanged += new ICollectionEvents_OnCollectionChangedEventHandler( collectionEvents_OnCollectionChanged ); }