Over in java I'm pretty used to working with generics and the wildcard. Things like: List<? extends Animal>
. This allows you to have a collection of subtypes of Animals and run generic routines on each element (e.g. makeNoise()
). I'm trying to accomplish this in C# but am a bit confused, since there's no wildcards.
Domain wise, what we're doing here is working with the SQL SMO libraries to collect scripts out of our database. We've got a base interface type that is extended a number of times to script and collect different objects (table, view, function, etc -- this is the T)
public interface IScripter<T> where T : IScriptable
{
IList<T> CollectScripts(params...)
}
public abstract class AbstractScripter<T> : IScripter<T> where T : IScriptable
{
....
}
public class TableScripter : AbstractScripter<Table>
{
....
}
public class ViewScripter : AbstractScripter<View>
{
....
}
So far so good. Seems like a perfectly sensible object hierarchy right? Here's what I intended to do, until I found out there's no wildcards:
public class Program
{
static void Main(string[] args)
{
// auto discover all scripter modules, table, view, etc
IList<Iscripter<? extends IScriptable>> allScripters = GetAllScripterModules();
foreach (IScripter<? extends IScriptable> scripter in allScripters)
{
IList<? extends IScriptable> scriptedObjects = scripter.CollectScripts(...);
// do something with scripted objects
}
}
}
Now since <? extends IScriptable>
doesn't exist here, what am I supposed to do instead? I've tried a number of things, generic method, just using the base type, all sorts of nasty casting, but nothing really did the trick.
What would you suggest to replace the IList<Iscripter<? extends IScriptable>
piece?
TIA