I have an interface IBakeable<T>
which has an event OnCooked
A couple of data classes Pie
and Bread
And I have a classes which implements IBakeable<Pie>
and IBakeable<Bread>
In order to implement this I think I have to implement the interfaces explicitly
public class BaseHeatSource: IBakeable<Pie>, IBakeable<Bread>
{
private event EventHandler OnPieCooked;
event EventHandler IBakeable<Pie>.OnCooked
{
add {OnPieCooked+= value;}
remove {OnPieCooked-= value;}
}
private event EventHandler OnBreadCooked;
event EventHandler IBakeable<Bread>.OnCooked
{
add {OnBreadCooked+= value;}
remove {OnBreadCooked-= value;}
}
}
And the class is inherited
public class Oven: BaseHeatSource
{
}
public class Fire: BaseHeatSource
{
}
Now I get the equivalent of:
CA1033 Interface methods should be callable by child types
Make 'BaseHeatSource' sealed (a breaking change if this class has previously shipped), implement the method non-explicitly, or implement a new method that exposes the functionality of 'IBakeable.OnCooked.add(EventHandler)' and is visible to derived classes.
If the derived type re-implements (explicitly) the inherited interface method, the base implementation can no longer be accessed. The call through the current instance reference will invoke the derived implementation; this causes recursion and an eventual stack overflow.
Note that adding
protected void AddOnBreadCookedHandler(EventHandler handler)
{
this.OnBreadCooked += handler;
}
does not resolve the rule.
Do I have to suppress this rule? or is there a way of fixing it?
Solved this via Composition...
Created an abstract
Bakeable<T>
class asCreated an
IBakeable<Pie>
property which is theBakeable<Pie>
classfor firing the event called
BakeablePie.Cooked
;for attaching to the event it simply became
oven.BakeablePie.OnCooked += customerWantsPie.Eat
I'm really not sure how this helps me in regard to Hans' comment in order to cook more things I need to add more properties