From 46949881772ffac272219fb36ed3ec2052b3b284 Mon Sep 17 00:00:00 2001 From: dahall Date: Sun, 28 Mar 2021 20:12:48 -0600 Subject: [PATCH] Enhanced ComConnectionPoint --- Core/InteropServices/ComConnectionPoint.cs | 34 +++++++++++++++--------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/Core/InteropServices/ComConnectionPoint.cs b/Core/InteropServices/ComConnectionPoint.cs index e1443d6e..15b5ed23 100644 --- a/Core/InteropServices/ComConnectionPoint.cs +++ b/Core/InteropServices/ComConnectionPoint.cs @@ -12,7 +12,7 @@ namespace Vanara.InteropServices /// public class ComConnectionPoint : IDisposable { - private List> connectionPoints = new List>(); + private readonly List<(IConnectionPoint, int)> connectionPoints = new List<(IConnectionPoint, int)>(); /// Initializes a new instance of the class. /// The COM object from which to query the reference. @@ -20,19 +20,20 @@ namespace Vanara.InteropServices /// The interfaces supported by that support events. public ComConnectionPoint(object source, object sink, params Type[] interfaces) { - if (source == null) throw new ArgumentNullException(nameof(source)); - if (sink == null) sink = this; - if (interfaces == null) interfaces = GetComInterfaces(sink); + if (source is null) throw new ArgumentNullException(nameof(source)); + if (source is not IConnectionPointContainer connectionPointContainer) + throw new ArgumentException("The source object must be COM object that supports the IConnectionPointContainer interface.", nameof(source)); + Sink = sink ?? this; + if (interfaces == null) interfaces = GetComInterfaces(Sink); if (interfaces.Length < 1) throw new ArgumentOutOfRangeException(nameof(interfaces)); // Start the event sink - if (!(source is IConnectionPointContainer connectionPointContainer)) throw new InvalidOperationException("The source object must be COM object that supports the IConnectionPointContainer interface."); foreach (var i in interfaces) { var comappEventsInterfaceId = i.GUID; connectionPointContainer.FindConnectionPoint(ref comappEventsInterfaceId, out var connectionPoint); - connectionPoint.Advise(sink, out var cookie); - connectionPoints.Add(new Tuple(connectionPoint, cookie)); + connectionPoint.Advise(Sink, out var cookie); + connectionPoints.Add((connectionPoint, cookie)); } } @@ -45,16 +46,14 @@ namespace Vanara.InteropServices /// public ComConnectionPoint(object source, object sink) : this(source, sink, null) { } - private static Type[] GetComInterfaces(object sink) - { - var ii = sink?.GetType().GetInterfaces().ToList() ?? new List(); - for (int i = ii.Count - 1; i >= 0; i--) - { - if (!ii[i].GetCustomAttributes(true).Any(a => a.GetType() == typeof(ComImportAttribute) || a.GetType() == typeof(InterfaceTypeAttribute))) - ii.RemoveAt(i); - } - return ii.ToArray(); - } + /// Gets the sink. + /// The sink. + public object Sink { get; private set; } + + private static Type[] GetComInterfaces(object sink) => + sink?.GetType().GetInterfaces(). + Where(i => i.GetCustomAttributes(true).Any(a => a.GetType() == typeof(ComImportAttribute) || a.GetType() == typeof(InterfaceTypeAttribute))). + ToArray(); /// Releases unmanaged and - optionally - managed resources. public void Dispose() @@ -65,6 +64,7 @@ namespace Vanara.InteropServices pair.Item1.Unadvise(pair.Item2); } connectionPoints.Clear(); + Sink = null; } } } \ No newline at end of file