I'm having some trouble with a generic method I'm writing. It has the following signature;
public static ThingCollection<T> GetThings<T>(...) where T : Thing
There are several classes; ThingA, ThingB and ThingC that inherit from Thing; and I want to be able to have code something like this in the method.
var things = new ThingCollection<T>();
if (typeof(T) == typeof(Thing))
foreach (var item in someCollection)
things.Add((T)new Thing(...));
else if (typeof(T) == typeof(ThingA))
foreach (var item in someCollection)
things.Add((T)new ThingA(...));
else if (typeof(T) == typeof(ThingB))
foreach (var item in someCollection)
things.Add((T)new ThingB(...));
else if (typeof(T) == typeof(ThingC))
foreach (var item in someCollection)
things.Add((T)new ThingC(...));
else
throw new Exception("Cannot return things of type " + typeof(T).ToString());
return things;
The problem is that I get a best overloaded method match has invalid arguments error if I don't cast the new objects. Adding the T casts as shown above is fine for the new Thing() but reports Cannot convert type 'ThingA' to 'T' for the other new calls. Intellisense indicates that T is a Thing but I don't understand why I can't cast the other objects to Thing, as they inherit from it.
Perhaps this is not the right way to be doing what I'm trying to do. Am I on the right track? Perhaps missing some small nuance, or should I be doing something else entirely?