downcast, Linq, .
foreach (Monkey m in myAnimals.OfType<Monkey>())
Program.FeedAnimal(m);
. - , FeedAnimal. Feed, FeedAnimal, this.
, Dictionary :
private static Dictionary<Type, Action<Animal>> _feeders;
, , :
_feeders[typeof(Monkey)] =
a =>
{
Monkey m = (Monkey)a;
};
, . , :
public static void AddFeeder<TAnimal>(Action<TAnimal> feeder) where TAnimal : Animal
{
_feeders[typeof(TAnimal)] = a => feeder((TAnimal)a);
}
, :
AddFeeder<Monkey>(monkey => GiveBananasTo(monkey));
AddFeeder<Dog>(dog => ThrowBiscuitsAt(dog));
, , :
public static void Feed(this Animal a)
{
_feeders[a.GetType()](a);
}
.
a.Feed();
, _feeders , , AddFeeder.
: , :
public static class AnimalFeeding
{
private static Dictionary<Type, Action<Animal>> _feeders
= new Dictionary<Type, Action<Animal>>();
public static void AddFeeder<TAnimal>(Action<TAnimal> feeder)
where TAnimal : Animal
{
_feeders[typeof(TAnimal)] = a => feeder((TAnimal)a);
}
public static void Feed(this Animal a)
{
for (Type t = a.GetType(); t != null; t = t.BaseType)
{
Action<Animal> feeder;
if (_feeders.TryGetValue(t, out feeder))
{
feeder(a);
return;
}
}
throw new SystemException("No feeder found for " + a.GetType());
}
}
t - , .