mirror of https://github.com/dahall/Vanara.git
Enhanced ComConnectionPoint
parent
73aa0c5859
commit
4694988177
|
@ -12,7 +12,7 @@ namespace Vanara.InteropServices
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ComConnectionPoint : IDisposable
|
public class ComConnectionPoint : IDisposable
|
||||||
{
|
{
|
||||||
private List<Tuple<IConnectionPoint, int>> connectionPoints = new List<Tuple<IConnectionPoint, int>>();
|
private readonly List<(IConnectionPoint, int)> connectionPoints = new List<(IConnectionPoint, int)>();
|
||||||
|
|
||||||
/// <summary>Initializes a new instance of the <see cref="ComConnectionPoint"/> class.</summary>
|
/// <summary>Initializes a new instance of the <see cref="ComConnectionPoint"/> class.</summary>
|
||||||
/// <param name="source">The COM object from which to query the <see cref="IConnectionPointContainer"/> reference.</param>
|
/// <param name="source">The COM object from which to query the <see cref="IConnectionPointContainer"/> reference.</param>
|
||||||
|
@ -20,19 +20,20 @@ namespace Vanara.InteropServices
|
||||||
/// <param name="interfaces">The interfaces supported by <paramref name="source"/> that support events.</param>
|
/// <param name="interfaces">The interfaces supported by <paramref name="source"/> that support events.</param>
|
||||||
public ComConnectionPoint(object source, object sink, params Type[] interfaces)
|
public ComConnectionPoint(object source, object sink, params Type[] interfaces)
|
||||||
{
|
{
|
||||||
if (source == null) throw new ArgumentNullException(nameof(source));
|
if (source is null) throw new ArgumentNullException(nameof(source));
|
||||||
if (sink == null) sink = this;
|
if (source is not IConnectionPointContainer connectionPointContainer)
|
||||||
if (interfaces == null) interfaces = GetComInterfaces(sink);
|
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));
|
if (interfaces.Length < 1) throw new ArgumentOutOfRangeException(nameof(interfaces));
|
||||||
|
|
||||||
// Start the event sink
|
// 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)
|
foreach (var i in interfaces)
|
||||||
{
|
{
|
||||||
var comappEventsInterfaceId = i.GUID;
|
var comappEventsInterfaceId = i.GUID;
|
||||||
connectionPointContainer.FindConnectionPoint(ref comappEventsInterfaceId, out var connectionPoint);
|
connectionPointContainer.FindConnectionPoint(ref comappEventsInterfaceId, out var connectionPoint);
|
||||||
connectionPoint.Advise(sink, out var cookie);
|
connectionPoint.Advise(Sink, out var cookie);
|
||||||
connectionPoints.Add(new Tuple<IConnectionPoint, int>(connectionPoint, cookie));
|
connectionPoints.Add((connectionPoint, cookie));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,16 +46,14 @@ namespace Vanara.InteropServices
|
||||||
/// </param>
|
/// </param>
|
||||||
public ComConnectionPoint(object source, object sink) : this(source, sink, null) { }
|
public ComConnectionPoint(object source, object sink) : this(source, sink, null) { }
|
||||||
|
|
||||||
private static Type[] GetComInterfaces(object sink)
|
/// <summary>Gets the sink.</summary>
|
||||||
{
|
/// <value>The sink.</value>
|
||||||
var ii = sink?.GetType().GetInterfaces().ToList() ?? new List<Type>();
|
public object Sink { get; private set; }
|
||||||
for (int i = ii.Count - 1; i >= 0; i--)
|
|
||||||
{
|
private static Type[] GetComInterfaces(object sink) =>
|
||||||
if (!ii[i].GetCustomAttributes(true).Any(a => a.GetType() == typeof(ComImportAttribute) || a.GetType() == typeof(InterfaceTypeAttribute)))
|
sink?.GetType().GetInterfaces().
|
||||||
ii.RemoveAt(i);
|
Where(i => i.GetCustomAttributes(true).Any(a => a.GetType() == typeof(ComImportAttribute) || a.GetType() == typeof(InterfaceTypeAttribute))).
|
||||||
}
|
ToArray();
|
||||||
return ii.ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Releases unmanaged and - optionally - managed resources.</summary>
|
/// <summary>Releases unmanaged and - optionally - managed resources.</summary>
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
@ -65,6 +64,7 @@ namespace Vanara.InteropServices
|
||||||
pair.Item1.Unadvise(pair.Item2);
|
pair.Item1.Unadvise(pair.Item2);
|
||||||
}
|
}
|
||||||
connectionPoints.Clear();
|
connectionPoints.Clear();
|
||||||
|
Sink = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue