I would like to only implement certain interfaces within other interfaces, I don't want them to be able to be inherited directly by a class.
Thanks in advance!
You can't do this in C# - any class can implement any interface it has access to.
Why would you want to do this? Bear in mind that by declaring an interface inheritance:
public interface InterfaceA {}
public interface InterfaceB : InterfaceA {}
You're specifying that anything implementing InterfaceB
also has to implement InterfaceA
, so you'll get classes implementing InterfaceA
anyway.
First of all, it doesn't make sense to say "implement within other interfaces" because interfaces can't implement anything.
I can see two flawed ways of doing this, sort of.
Make Animated and NonAnimated abstract classes that implement IAnimation. The concrete class below them can still forcibly override your IAnimation methods with the new operator:
class SomeAnim : Animated
{
public new void Foo() { }
}
Use mixins. Keep IAnimated and INonAnimated as interfaces, but don't put any methods in your interface. Instead define extension methods like this:
static class Ext
{
public static void Foo(this IAnim anim)
{
if (anim is IAnimated) // do something
else if (anim is INonAnimated) // do something else
}
}
again, a bit of a hack. But what you're trying to do indicates design flaws anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With