title: Return specific type in generic method depending on enum value without Reflection or dynamic during runtime
body: <p>I have a non-generic <code>IList</code> which i would like to cast depending on an <code>enum</code> value during runtime.</p>
  <p><strong>I can not change the type of the non-generic IList in the implementation</strong>, because it is library code.</p>
  <p><strong>Also i do not want to use Reflection (because it is slow) or the dynamic keyword (because it is unsafe and could lead to mistakes down the road).</strong></p>
  <p>In the Library Code there is following class:</p>
  <pre><code>public class ListView //THIS IS LIBRARY CODE AND CAN NOT BE CHANGED
  {
    public IList itemsSource { get; set; }
  }
  </code></pre>
  <p>Then in my CustomListView class which inherits from ListView i want to cast the itemsSource to the appropiate class depending on ItemType.</p>
  <p><strong>Another limitation is that CustomListView can not be generic (dictated by the library we use)</strong></p>
  <pre><code>public class CustomListView : ListView
  {
      public dynamic GetItems()
      {
          switch (ItemType)
          {
              case ItemType.A:
                return itemsSource.Cast&lt;A&gt;().ToList();
              case ItemType.B:
                return itemsSource.Cast&lt;B&gt;().ToList();
              default:
                throw new InvalidOperationException(&quot;Not supported&quot;);
          }
      }
  }
  </code></pre>
  <p>But i want it to directly return the correct type instead of using dynamic.</p>
  <p>Something like (the code below does not work!):</p>
  <pre><code>public IList&lt;T&gt; GetItems(ItemType itemType) //The type of T should change depending on what i return
  {
      switch (ItemType)
      {
          case ItemType.A:
            return itemsSource.Cast&lt;A&gt;().ToList();
          case ItemType.B:
            return itemsSource.Cast&lt;B&gt;().ToList();
          default:
            throw new InvalidOperationException(&quot;Not supported&quot;);
      }
  }
  </code></pre>
  <p>How would i implement that?</p>
  <p><strong>Edit/Appendage</strong></p>
  <p>As you guys pointed out i should clarify some things.</p>
  <p>Class A and B do indeed have the same base class.
  But i want to be able to not cast it from the base type again (because i already cast it in the GetItems() method and also the value of ItemType is known).</p>
  <p>I want to be able to do following</p>
  <pre><code>IList&lt;A&gt; aItems = listView.GetItems()
  </code></pre>
  <p>Without casting.</p>
  <p>The idea behind all of this is to have a general CustomListView which can handle multiple item types. Those items will be added to the itemSource. The type of those items is determined by the ItemType.</p>
  <p>I use it like this for example</p>
  <pre><code>public class UiForClassA
  {
    public void Foo()
    {
      CustomListView customListView = new CustomListView(ItemType.A);
  
      IList&lt;A&gt; itemsOfCustomListView = customListView.GetItems(); //No cast needed because it should somehow implicitly know that.
    }
  }
  </code></pre>
  <p><strong>I do not want to use Casts everywhere where i use the CustomListView. It somehow should implicitly return the correct items.</strong></p>
  <p>And for your information i use the Unity UI Toolkit Framework, but that is not really relevant to the Question.</p>