Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Solution #3: by-far the best, no need to cast anything, no need to ask the type of the input list (repeatedly), much much cleaner. Please compare with Answer/Solution #2 above. </p> <p><strong>(#3)</strong> accomplishes exactly the goal I was seeking, common code for the logic that process either IXList or IYList. And as an extension method the user gets the 'GetAllItems' typed version desired...</p> <pre><code> internal static IEnumerable&lt;IXOutList&gt; GetAllLineItems(this IXList list) { TListEnumeratorBase genList = new IXListEnum(list); return GetAllLineItemsGeneric&lt;IXOutList&gt;(genList); } internal static IEnumerable&lt;IYOutList&gt; GetAllLineItems(this IYList list) { TListEnumeratorBase genList = new IYListEnum(list); return GetAllLineItemsGeneric&lt;IYOutList&gt;(genList); } private static IEnumerable&lt;Zout&gt; GetAllLineItemsGeneric&lt;Zout&gt;(TListEnumeratorBase genList) where Zout : class { try { genList.Head(); } catch (Exception) { yield break; } Guid guid = Marshal.GenerateGuidForType(typeof(Zout)); do { Zout rec = null; try { IntPtr lineItemPointer; lineItemPointer = genList.GetTxnItem(ref guid); rec = Marshal.GetObjectForIUnknown(lineItemPointer) as Zout; } catch (Exception) { yield break; } if (rec != null) yield return rec; else yield break; try { genList.Next(); } catch (Exception) { yield break; } } while (true); } public abstract class TListEnumeratorBase { public abstract void Head(); public abstract void Next(); public abstract IntPtr GetTxnItem(ref Guid guid); } public class IXListEnum : TListEnumeratorBase { IXList _list; public IXListEnum(IXList list) { _list = list; } public override void Head() { _list.Head(); } public override void Next() { _list.Next(); } public override IntPtr GetTxnItem(ref Guid guid) { IntPtr lineItemPointer; _list.Get(ref guid, out lineItemPointer); return lineItemPointer; } } public class IYListEnum : TListEnumeratorBase { IYList _list; public IYListEnum(IYList list) { _list = list; } public override void Head() { _list.Head(); } public override void Next() { _list.Next(); } public override IntPtr GetTxnItem(ref Guid guid) { IntPtr lineItemPointer; _list.Get(ref guid, out lineItemPointer); return lineItemPointer; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload