How to tell if an interface method has been implemented

C

Chris Dunaway

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
 
J

Jon Skeet [C# MVP]

Chris Dunaway said:
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.
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.
 
M

Mattias Sjögren

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?

Sure you can create your own attribute to indicate that, but I don't
see the point.

2. Should I just call MethodB as if it had been implemented?

Yeah, why not?

Is there
any performance issues with calling an empty sub?

Well it will likely execute faster than any method that has a code
body. I wouldn't call that an issue though.



Mattias
 
O

Oliver Sturm

Jon said:
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
 
J

Jon Skeet [C# MVP]

Oliver Sturm said:
... 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.
 
O

Oliver Sturm

Jon said:
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
 
C

Chris Dunaway

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top