I have an interface, which in this example we'll call IMagicPerson
public interface class IMagicPerson : IPerson
{
IEnumerable<MagicPower> Powers { get; }
}
A few things inherit IMagicPerson
, and then some other classes derive from them too, and so on, causing some pretty long derivative hierarchy. Each of these needs to be able to declare some of these powers, but must also use powers from the derived class.
I've played around with various ways of doing this. I've used:
- A
MagicPowerCollection
class that can enumerate through multiple MagicPower enumerators as if it were one enumerator (usingyield return
statements) and using this to add aprivate static MagicPower[]
tobase.Powers
- Using another constructor of the
MagicPowerCollection
that can add single Powers to the end of the enumerator (again, usingyield returns
)
But the way I'd really prefer to go about this would be by
- Overloading the + operator of IEnumerable to allow adding more enumerators or single Powers
Is this a bad way to do it? I think it looks quite nice. For example, I could have:
public class Wizard : IMagicPerson
{
public override IEnumerable<MagicPower> Powers
{
get
{
AllPowers.AntiGravity + AllP开发者_Go百科owers.BroomStick;
//note: AllPowers, in this example, is a static class and all the properties return a MagicPower or type derived from MagicPower
}
}
}
public class EvilWizard : Wizard
{
public override IEnumerable<MagicPower> Powers
{
get
{
return base.Powers + AllPowers.InstantDeath + AllPowers.MindControl;
}
}
}
So, where and how would I overload the +
operator in such a way? I need to be able to add a MagicPower
to either an enumerator, or to another MagicPower
. Both of which will return an IEnumerator<MagicPower>
.
I'll also accept other suggestions on other ways of achieving this, which don't including overloading this operator.
Notes:
Bare in mind I'm using C# 2.0, so extension methods won't work (although I'm strongly considering upgrading this server just for this).
Another complication is that although the powers are usually the same for each type of MagicPerson
, there are also some powers that can only used in certain situations (for example, some Wizards
can't use powers in the Muggle world :D). I'm not asking how to do this, I can just use if
statements or add these properties somewhere else. Just letting people know that a completely static solution won't work.
Another thing to bare in mind is that I do not have access to the IMagicPerson interface, so I can't even get the property to return a new class that inherits IEnumerable and put the overloaded operators in there (or can I?)
Thanks!!
It is completely impossible to override an operator on an interface, in any version.
You should make a static
method that takes a params IEnumerable<T>[]
and yield return
s all of them using a double-foreach
.
To suggest an answer to my own question:
I could create a base class for each magic person:
public abstract class MagicPerson : IMagicPerson
{
protected List<MagicPower> powers;
public IEnumerable<MagicPower> Powers { get { return powers; } }
}
Then add to this powers
list in the constructors of the derived classes.
精彩评论