Sorry for the vague name, but I was not sure how to summarize this in one phrase. I have a situation with a lot of redundant C # code, and actually it looks like some kind of ingenious trick using some kind of inheritance property or generics. However, I'm not a terribly experienced programmer (especially with C #) and just can't see the solution.
The situation in a simplified form looks something like this. I have a bunch of classes that all inherit from one type.
public class Foo : SuperFoo
{
...
public Foo SomeMethod() { ... }
}
public class Bar : SuperFoo
{
...
public Bar SomeMethod() { ... }
}
public class Baz : SuperFoo
{
...
public Baz SomeMethod() { ... }
}
...
public class SuperFoo
{
...
}
The problem arises when collections of these objects need to be processed. My first solution (bad) is as follows:
public void SomeEventHasHappened(...)
{
ProcessFoos();
ProcessBars();
ProcessBazes();
...
}
public void ProcessFoos()
{
...
foreach (var foo in fooList)
{
...
foo.SomeMethod();
}
}
public void ProcessBars()
{
...
foreach (var bar in barList)
{
...
bar.SomeMethod();
}
}
... . , ProcessX , , . .
, Process(), List<SuperFoo> . , SuperFoo SomeMethod(), , SomeMethod() , .