|
-
May 8th, 2009, 12:18 PM
#1
Thread Starter
Not NoteMe
[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!!
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
May 8th, 2009, 01:57 PM
#2
Frenzied Member
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.
-
May 8th, 2009, 02:03 PM
#3
Thread Starter
Not NoteMe
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++
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
May 13th, 2009, 08:10 AM
#4
Re: [RESOLVED] Classes using an interfaces extension method
You could consider that you don't make it a interface, but anabstract class.
-
May 13th, 2009, 11:57 AM
#5
Thread Starter
Not NoteMe
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.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|