473,387 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to tell if an interface method has been implemented

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

Aug 25 '05 #1
6 2020
Chris Dunaway <du******@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Aug 25 '05 #2
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 25 '05 #3
Jon Skeet [C# MVP] wrote:
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
Aug 26 '05 #4
Oliver Sturm <ol****@sturmnet.org> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Aug 26 '05 #5
Jon Skeet [C# MVP] wrote:
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
Aug 26 '05 #6
> 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

Aug 26 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by...
12
by: Steve W. | last post by:
I just read the section (and did the exercise) in the C# Step by Step book that covers Explict Interface Implementation (where you specify in the method implementation the specific interface that...
2
by: chan | last post by:
Hi, I created a class that implements the System.Collections.IDictionary interface and I declared methods for implementing IDictionary, and also the interfaces it inherited from, include...
16
by: Daniel Mori | last post by:
If an object implements the IDisposable interface (regardless if its a framework object or a user object), should I always dispose of that object out of principle?
2
by: Oenone | last post by:
I could use a little advice to help prevent me making a possible mess of a project. :) In VB6, I once created a project that exposed a public interface class. I then Implemented this in various...
6
by: Chris Dunaway | last post by:
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,...
2
by: Lee | last post by:
Assuming I have an interface defined: public interface IDTPersistableCollection: IList<IDTPersistableObject> { //Few specific properties defined } When I try to create an object based on...
15
by: Gustaf | last post by:
Using VS 2005. I got an 'IpForm' class and an 'IpFormCollection' class, containing IpForm objects. To iterate through IpFrom objects with foreach, the class is implemented as such: public class...
2
by: Ashish Gupra | last post by:
Hi, Is there any method to know the interface name through which the class has been implemented? Example: Public Interface I1 Public funcation Test() as string End Interface
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.