From 3863d931a0ad77efd6fc9b11c323c160e8cfb8c6 Mon Sep 17 00:00:00 2001 From: dahall Date: Mon, 27 Dec 2021 08:36:41 -0700 Subject: [PATCH] Fixed IEnumFromCom change that broke elements without default construtors. --- PInvoke/Shared/Collections/IEnumFromCom.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/PInvoke/Shared/Collections/IEnumFromCom.cs b/PInvoke/Shared/Collections/IEnumFromCom.cs index a2ebd628..a7d1883e 100644 --- a/PInvoke/Shared/Collections/IEnumFromCom.cs +++ b/PInvoke/Shared/Collections/IEnumFromCom.cs @@ -49,18 +49,21 @@ namespace Vanara.Collections /// if a class doesn't support or like some COM objects. /// /// The type of the item. - public class IEnumFromCom : IEnumFromNext where TItem : new() + public class IEnumFromCom : IEnumFromNext { private readonly ComTryGetNext cnext; + private readonly Func make; /// Initializes a new instance of the class. /// The method used to try to get the next item in the enumeration. /// The method used to reset the enumeration to the first element. - public IEnumFromCom(ComTryGetNext next, Action reset) : base() + /// The method used to create the default value placed in the array for . + public IEnumFromCom(ComTryGetNext next, Action reset, Func makeNew = null) : base() { if (next is null || reset is null) throw new ArgumentNullException(); cnext = next; + make = makeNew ?? DefaultMaker; base.next = TryGet; base.reset = reset; } @@ -88,9 +91,11 @@ namespace Vanara.Collections return new IEnumFromCom(cew.ComObjTryGetNext, cew.ComObjReset); } + private static TItem DefaultMaker() => default; + private bool TryGet(out TItem item) { - var res = new TItem[] { new TItem() }; + var res = new TItem[] { make() }; item = default; if (cnext(1, res, out var ret) != HRESULT.S_OK) return false;