[RESOLVED] Classes using an interfaces extension method
Hi all,
I have an interface (IMessageDispatcher) which has an extension method (DispatchMessages) containing code that would be identical in all classes that implement it.
I don't want to code the identical method in every class that implements the interface.
If i leave out the method in the class i get a compiler error, class does not implement interface, but i don't want to implement it as there is an extension method already there.
I've tried casting to the interface to call the extension method, but it still calls my class' wrapper method, so i get a stack overflow:
Code:
public void DispatchMessages()
{
(this as IMessageDispatcher).DispatchMessages();
}
How can i avoid writing identical code in all classes that implement this interface?
Cheers!!
Re: Classes using an interfaces extension method
DispatchMessages() should no longer be a method of IMessageDispatcher since DispatchMessages() is now an extension method.
When you implement IMessageDispatcher you won't have to define DispatchMessages() in the class.
You can't have the same method name in an interface and also an extension of that interface.
Re: Classes using an interfaces extension method
Thanks for the very quick reply.
Now that i think about it, it is kind of obvious, it's the first time i've used extension methods on interfaces!
Cheers for the heads up. Rep++
Re: [RESOLVED] Classes using an interfaces extension method
You could consider that you don't make it a interface, but anabstract class.
Re: [RESOLVED] Classes using an interfaces extension method
I couldn't use a base/abstract class because the classes that would implement the interfaces are already inheriting, but thanks for the suggestion. :)