Adding derived objects to a list dynamically
<p>I have an inheritance tree like so:</p>

<pre><code>BaseType
TypeA : BaseType
TypeB : BaseType
TypeC : BaseType
</code></pre>

<p>Each derived object has a variable named <code>objectName</code> that defines that object's name.</p>

<p>I also have an array and a list that both hold the<code>BaseType</code> objects as well as any objects derived from <code>BaseType</code>. The array has objects stored in it upon being created, meanwhile the list is empty for future use:</p>

<pre><code>BaseType[] arrayA = new BaseType[] { new TypeA(), new TypeB(), new TypeC(), }

List&lt;BaseType&gt; listA = new List&lt;BaseType&gt;;
</code></pre>

<p>I also have a method that is used to add an object that is in the array to the list:</p>

<pre><code>public void AddToList(BaseType itemToAdd)
{
    if(itemToAdd.objectName == "Type A")
        {
        listA.Add(new TypeA());
    }
    else if(itemToAdd.objectName == "Type B")
        {
        listA.Add(new TypeB());
    }
    else if(itemToAdd.objectName == "Type C")
        {
        listA.Add(new TypeA());
    }
}
</code></pre>

<p>How can I avoid having to use all of those <code>if</code> commands? At one point I tried:</p>

<pre><code>public void AddToList(BaseType itemToAdd)
{
    listA.Add( new itemToAdd());
}
</code></pre>

<p>Which did not work. So how can I go about doing something like this? The objects are constantly growing and I don't want to have to put an <code>if</code> for every derived object I add. I should also mention, not sure if this will have any bearing on the solution, I've been using folders to organize the .cs files. So I have all of the derived classes in a folder named "Types" so to create a new instance of that object I have to type:</p>

<pre><code>listA.Add(new Types.TypeA());
</code></pre>
