Well if the Gribble
, Gribulator
, and Knepple
classes share a common base type or implement a common interface, you can just do this:
public Func<ObservableCollection<ICommonInterface>> MakeFunc(ListNames listName)
{
...
}
If they don't have a common base type or interface, you'd have to do this:
public Func<ICollection> MakeFunc(ListNames listName)
{
...
}
Which, admittedly, is not a very good solution. Note: you can replace ICollection
with INotifyCollectionChanged
or whatever non-generic interface that you need most.
In both cases, you have to rely on the caller to specify the data type (even if the caller specifies the enum value as well). So, you could also get rid of the enum, and go for something like this:
public Func<ObservableCollection<T>> MakeFunc<T>()
{
if (typeof(T) == typeof(Gribble))
return () => GetGribbles() as ObservableCollection<T>;
if (typeof(T) == typeof(Gribulator))
return () => GenerateGribulators() as ObservableCollection<T>;
if (typeof(T) == typeof(Knepple))
return () => MakeKnepples() as ObservableCollection<T>;
throw new ArgumentException("Unknown list name");
}
Then the caller could simply write:
var gribblesFunc = MakeFunc<Gribble>();