Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this:</p> <pre><code>var someGenericListYouCreated = ...; var enumerable = someGenericListYouCreated as IEnumerable; foreach(var foo in enumerable){ ... } </code></pre> <p>However, I'm working on way to do what you ACTUALLY want.</p> <p>Edit:</p> <p>Right, I hope this makes sense</p> <pre><code>private class Adapter&lt;T&gt; { private readonly Action&lt;object&gt; act; public Adapter(Action&lt;object&gt; act){ this.act = act; } public void Do(T o) { act(o); } } public static void Main(string[] args) { Type elementType = typeof(string); var genericType = typeof(List&lt;&gt;).MakeGenericType(elementType); var list = Activator.CreateInstance(genericType); var addMethod = list.GetType().GetMethod("Add"); addMethod.Invoke(list, new object[] { "foo" }); addMethod.Invoke(list, new object[] { "bar" }); addMethod.Invoke(list, new object[] { "what" }); Action&lt;object&gt; printDelegate = o =&gt; Console.WriteLine(o); var adapter = Activator.CreateInstance(typeof(Adapter&lt;&gt;).MakeGenericType(elementType), printDelegate); var adapterDo = adapter.GetType().GetMethod("Do"); var adapterDelegate = Delegate.CreateDelegate(typeof(Action&lt;string&gt;), adapter, adapterDo); var foreachMethod = list.GetType().GetMethod("ForEach"); foreachMethod.Invoke(list, new object[] { adapterDelegate }); } </code></pre> <p>What this does:</p> <ol> <li>Create a generic list (notice that it's using typeof(List&lt;>)</li> <li>Add some strings to this list</li> <li>Create a new delegate, in this case an Action</li> <li>Create an instance of the adapter class</li> <li>Create a delegate of the type we need (Action) on the adapter class</li> <li>Call ForEach</li> </ol> <p>Note, you don't necessarily have to use Action if you know the type you're going to be processing. You could very easily use an Action and it'll work... </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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