I’m trying the following design without success:
abstract class Foo<T>
{
abstract T Output { get; }
}
class Bar
{
List<Foo> Foos;
}
I would dislike using an array list, because I would have to use unsafe casts to get the type. I’d like Foo to be typed so that “Output” isn’t just an object, in which case I’d have to use unsafe casts as well.
As my code is at the moment, I use Foo untyped with Output as an object.
If I understand you correctly, you want a list of
Fooobjects which have different types ofOutputs, right? Since these outputs are of different types, you would have to use casts here anyway.However, the following idea might help. How about you declare a non-generic interface called
IFoo: ¹and then implement it in your abstract class:
Then you can declare a list of
IFoos:When accessing those foos, you can either retrieve the output as an object via the interface:
or you can try to discover the real type if you know that it can only be one of a few specific types:
You can even do filtering based on the type, for example:
¹ You can also make this an abstract base class called
Fooand haveFoo<T>derive from it. In that case, you would need to markFoo<T>.Outputwith thenewkeyword and usebase.Outputto access the abstract base member.