473,669 Members | 2,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Met hodB()
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 2036
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.Met hodB()
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
StubImplementat ionAttribute) to the stub implementation - then you
could look for that.

--
Jon Skeet - <sk***@pobox.co m>
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
StubImplementat ionAttribute) 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****@sturmne t.org> wrote:
One thing you could do is add a custom attribute which you'd create (eg
StubImplementat ionAttribute) 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.co m>
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
8188
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 making my contnent dlls implement an interface created in vb6. The viewer application is bound to this interface. This way, I am able to add Content without redeploying the dlls (I just have to add the new dlls). I want to write new content for...
12
2788
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 you are implementing in the class. Other than to resolve the problem that arises when a class implements two interfaces with the same method signature, what good is it?
2
1341
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 ICollection and IEnumerable. I have problem in compiling my class and the C# compiler complained about the method GetEnumerator(), which has
16
4250
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
1780
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 plug-in DLLs so that I could early-bind to the plug-ins by declaring objects of the interface class type. This worked fine, until one day I found that I needed to add a new method to the interface class. Of course, everything broke immediately...
6
276
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, 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)
2
1544
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 this, I get all kind of errors about members not being implemented such as this one:
15
2724
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 IpFormCollection : IEnumerable<IpForm> { ArrayList forms = new ArrayList(); public IEnumerator<IpFormGetEnumerator() {
2
1055
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
8466
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8384
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8590
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8659
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7410
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6211
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5683
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4387
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2798
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.