HighTechTalks DotNet Forums  

How to tell if an interface method has been implemented

Dotnet General Discussions microsoft.public.dotnet.general


Discuss How to tell if an interface method has been implemented in the Dotnet General Discussions forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Chris Dunaway
 
Posts: n/a

Default How to tell if an interface method has been implemented - 08-25-2005 , 03:33 PM






I have a simple interface with two methods, MethodA, and MethodB.

I also have a class with implements the interface. The class provides
a full implementation for MethodA but for special reasons, provides
only a stub for MethodB, with no code in it.

The code that uses the interface needs to be able to tell if MethodB
has actually been implemented by the class. For example:

(pseudocode)

If MethodBHasCode Then
MyInterface.MethodB()
Endif

My questions:

1. Is there any attribute I can apply to the stubbed method in the
class so that I can see if it has actually been implemented?

2. Should I just call MethodB as if it had been implemented? Is there
any performance issues with calling an empty sub?

Thanks


Reply With Quote
  #2  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: How to tell if an interface method has been implemented - 08-25-2005 , 04:41 PM






Chris Dunaway <dunawayc (AT) gmail (DOT) com> wrote:
Quote:
I have a simple interface with two methods, MethodA, and MethodB.

I also have a class with implements the interface. The class provides
a full implementation for MethodA but for special reasons, provides
only a stub for MethodB, with no code in it.

The code that uses the interface needs to be able to tell if MethodB
has actually been implemented by the class. For example:

(pseudocode)

If MethodBHasCode Then
MyInterface.MethodB()
Endif

My questions:

1. Is there any attribute I can apply to the stubbed method in the
class so that I can see if it has actually been implemented?
No - it *has* been implemented, just in a no-op way.

Quote:
2. Should I just call MethodB as if it had been implemented? Is there
any performance issues with calling an empty sub?
It's unlikely to be a bottleneck, certainly - I would only worry about
it when you've definitely got a problem.

One thing you could do is add a custom attribute which you'd create (eg
StubImplementationAttribute) to the stub implementation - then you
could look for that.

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Reply With Quote
  #3  
Old   
Oliver Sturm
 
Posts: n/a

Default Re: How to tell if an interface method has been implemented - 08-26-2005 , 04:09 AM



Jon Skeet [C# MVP] wrote:

Quote:
2. Should I just call MethodB as if it had been implemented? Is there
any performance issues with calling an empty sub?

It's unlikely to be a bottleneck, certainly - I would only worry about
it when you've definitely got a problem.

One thing you could do is add a custom attribute which you'd create (eg
StubImplementationAttribute) to the stub implementation - then you
could look for that.
.... but that would certainly take more time than just calling the empty
method.



Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog


Reply With Quote
  #4  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: How to tell if an interface method has been implemented - 08-26-2005 , 05:10 AM



Oliver Sturm <oliver (AT) sturmnet (DOT) org> wrote:
Quote:
One thing you could do is add a custom attribute which you'd create (eg
StubImplementationAttribute) to the stub implementation - then you
could look for that.

... but that would certainly take more time than just calling the empty
method.
It would take more time if you had to do it every time, yes. You'd want
to cache that information - indeed, you might end up putting objects
which only had stub implementations into a different collection, etc.
It all depends on the situation.

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Reply With Quote
  #5  
Old   
Oliver Sturm
 
Posts: n/a

Default Re: How to tell if an interface method has been implemented - 08-26-2005 , 06:47 AM



Jon Skeet [C# MVP] wrote:

Quote:
It would take more time if you had to do it every time, yes. You'd want
to cache that information - indeed, you might end up putting objects
which only had stub implementations into a different collection, etc.
It all depends on the situation.
I have never tested anything like this, but I imagine the time to call
an empty method to be extremely small - if the compiler doesn't optimize
away the call to begin with. I guess if you were going to use a single
bool flag per method and just checked it on later calls, you might save
some time. But as soon as any kind of more complicated lookup comes into
play, such as using collections of any kind, I can't believe you'll
actually save time compared to just calling the method.

Anyway, it's again one of these things: I'd just call the method, unless
the profiler shows me that this is a bad idea.



Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog


Reply With Quote
  #6  
Old   
Chris Dunaway
 
Posts: n/a

Default Re: How to tell if an interface method has been implemented - 08-26-2005 , 08:37 AM



Quote:
an empty method to be extremely small - if the compiler doesn't optimize
away the call to begin with. I guess if you were going to use a single
I did a little test, with optimizations enabled and without. WITH
optimizations enabled, An empty sub seems to have just a ret statement
but the call to it remains. WITHOUT optimizations, the empty sub seems
to have a couple of nop's and then a return and the call is still made.

Chris



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.