Note that there are some explanatory texts on larger screens.

plurals
  1. POTransforming a generic list of one type to another type, where the types are only known at runtime
    primarykey
    data
    text
    <p>Essentially, I'm looking for a way to transform <code>GenericList&lt;TInput&gt;</code> to <code>GenericList&lt;TOutput&gt;</code>, where <code>GenericList</code> is a generic list of any type that implements a specific interface, and the types <code>TInput</code> and <code>TOutput</code> are only known at runtime.</p> <p>Below is a snippet of a class and method that can perform this operation, where <code>TInput</code> and <code>TOutput</code> are supplied at compile time.</p> <pre><code> // -------------------------------------------------------------------------------- /// &lt;summary&gt;This class is an example snippet for transforming generic lists of different types.&lt;/summary&gt; /// /// &lt;remarks&gt;&lt;/remarks&gt; // -------------------------------------------------------------------------------- public abstract class GenericListHelper&lt;TInput, TOutput&gt; where TInput : IGenericObject, new() where TOutput : IGenericObject, new() { // -------------------------------------------------------------------------------- /// &lt;summary&gt;This method takes in a generic list of an input type /// and transforms it a list of the output type.&lt;/summary&gt; /// /// &lt;param name="inputGenericList"&gt;The input list to transform to this list.&lt;/param&gt; /// &lt;param name="filterElements"&gt;Field and property values to exclude from transform.&lt;/param&gt; // -------------------------------------------------------------------------------- public static GenericList&lt;TOutput&gt; CreateList(GenericList&lt;TInput&gt; inputGenericList, NameObjectCollection filterElements) { if (inputGenericList != null) { GenericList&lt;TOutput&gt; outputGenericList = new GenericList&lt;TOutput&gt;(); foreach (TInput loopItem in inputGenericList) { TOutput newItem = new TOutput(); DataTransformHelper.TransformDataFromObject(loopItem, newItem, filterElements); outputGenericList.Add(newItem); } return outputGenericList; } return null; } } </code></pre> <p>Is there any way of doing something along these lines where <code>TInput</code> and <code>TOutput</code> can be supplied at runtime?</p> <p>Using reflection in one form or another seems to be the path to get there.</p> <p>Initially, I tried creating a constructor for <code>GenericList&lt;TInput&gt;</code> that would take a list of type <code>TOutput</code> as a parameter (then I could call <code>Activator.CreateInstance</code> to get the new list).</p> <p>Alternatively, I tried invoking the above method through reflection, but since that method is flagged <code>ContainsGenericParameters=true</code>, and is flagged as <code>IsGenericMethod=false</code>, I was not able to invoke the method, either through a normal <code>method.Invoke</code> or through a generic <code>method.Invoke</code> (unable to call <code>MakeGenericMethod</code>).</p>
    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.
 

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