示例#1
0
 public void Disconnect()
 {
     if (this.connectionPoint != null && this.cookie != 0)
     {
         this.connectionPoint.Unadvise(cookie);
         this.cookie = 0;
         this.connectionPoint = null;
     }
 }
示例#2
0
            public ConnectionPointCookie(object source, object sink, Type eventInterface, bool throwException)
            {
                Exception ex = null;
				if (source is NativeMethods.IConnectionPointContainer) 
                {
					NativeMethods.IConnectionPointContainer cpc = (NativeMethods.IConnectionPointContainer)source;

                    try
                    {
                        Guid tmp = eventInterface.GUID;
                        cpc.FindConnectionPoint(ref tmp, out connectionPoint);
                    } 
                    catch(Exception) 
                    {
                        connectionPoint = null;
                    }

                    if (connectionPoint == null)
                    {
                        ex = new ArgumentException("The source object does not expose the " + eventInterface.Name + " event inteface");
                    }
                    else if (!eventInterface.IsInstanceOfType(sink)) 
                    {
                        ex = new InvalidCastException("The sink object does not implement the eventInterface");
                    }
                    else 
                    {
                        try 
                        {
                            connectionPoint.Advise(sink, out cookie);
                        }
                        catch 
                        {
                            cookie = 0;
                            connectionPoint = null;
                            ex = new Exception("IConnectionPoint::Advise failed for event interface '" + eventInterface.Name + "'");
                        }
                    }
                }
                else 
                {
                    ex = new InvalidCastException("The source object does not expost IConnectionPointContainer");
                }

                if (throwException && (connectionPoint == null || cookie == 0))
                {
                    if (ex == null) 
                    {
                        throw new ArgumentException("Could not create connection point for event interface '" + eventInterface.Name + "'");
                    }
                    else 
                    {
                        throw ex;
                    }
                }
            }