473,769 Members | 6,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I call base interface method?

jon
How can I call a base interface method?

class ThirdPartyClass :IDisposable { //I can not modify this class
void IDisposable.Dis pose() {
Console.WriteLi ne( "ThirdPartyClas s Dispose" );
}
}

class MyClass :ThirdPartyClas s, IDisposable {
void IDisposable.Dis pose() {
Console.WriteLi ne( "MyClass Dispose" );
//base.IDisposabl e.Dispose(); //How can I do this !!!!
}
}

Nov 17 '05
15 12791
jo*@martinsound .com wrote:
How can I call a base interface method?

class ThirdPartyClass :IDisposable { //I can not modify this class
void IDisposable.Dis pose() {
Console.WriteLi ne( "ThirdPartyClas s Dispose" );
}
}

class MyClass :ThirdPartyClas s, IDisposable {
void IDisposable.Dis pose() {
Console.WriteLi ne( "MyClass Dispose" );
//base.IDisposabl e.Dispose(); //How can I do this !!!!
}
}


Not easily, as you can't call an explicit interface implementation via
base.Dispose (which is the point of the question, I suppose).

Anyhow, try casting MyClass to its ancestral type, then casting the
ancestral type to IDispose, as below. Note, though, that
DisposableAnces tor.Dispose() calls MyClass.IDispos able.Dispose - which
makes sense, given that you could cast DisposableAnces tor back to
MyClass ....

class MyClass :ThirdPartyClas s, IDisposable
{
private bool Disposed;

void IDisposable.Dis pose()
{
if (Disposed) //DisposableAnces tor.Dispose() calls this method
return;
else
Disposed = true;

Console.WriteLi ne( "MyClass Dispose" );

IDisposable DisposableAnces tor = (IDisposable)
((ThirdPartyCla ss) this);
DisposableAnces tor.Dispose();
//base.IDisposabl e.Dispose(); //How can I do this !!!!
}
}
--

www.midnightbeach.com
Nov 17 '05 #11
>
IDisposable DisposableAnces tor = (IDisposable)
((ThirdPartyCla ss) this);
DisposableAnces tor.Dispose();
//base.IDisposabl e.Dispose(); //How can I do this !!!!
}
}


This won't work due to the reasons a gave in my post.
Nov 17 '05 #12
Cody,

The problem is that the method is not declared as virtual in the third party
class. Hence, the Dispose method is not virtual. (No VTable) Not the other
way around.

Frisky

"cody" <de********@gmx .de> wrote in message
news:uk******** ******@tk2msftn gp13.phx.gbl...
Since your implementation of IDisposable.Dis pose() overwrite the original
entry of Dispose in the vtable there is no way to get back the pointer to
the original Dispose method.
If you find no other solution for your problem you can try with
reflection.
<jo*@martinsoun d.com> schrieb im Newsbeitrag
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
How can I call a base interface method?

class ThirdPartyClass :IDisposable { //I can not modify this class
void IDisposable.Dis pose() {
Console.WriteLi ne( "ThirdPartyClas s Dispose" );
}
}

class MyClass :ThirdPartyClas s, IDisposable {
void IDisposable.Dis pose() {
Console.WriteLi ne( "MyClass Dispose" );
//base.IDisposabl e.Dispose(); //How can I do this !!!!
}
}


Nov 17 '05 #13
Implementation of an interface is not inheritence. There is nothing to
inherit. You are simply required to implement the specific signature as
required by the interface.

But, the problem is not that you are overwriting the VTable as cody
mentioned. There is not VTable entry for this item. That is because in the
third party class, the item was not marked as virtual. Ultimatley, this is
the problem. If the Dispose method in the third party class is marked as
virtual, then the method may be overwritten, and the base functionality may
be called. Try it, it works.

Since you can't change the third party class, you have to use a hack to get
around it.

"Jeff Louie" <je********@yah oo.com> wrote in message
news:OS******** ********@TK2MSF TNGP09.phx.gbl. ..
C# supports single inheritance of an implementation hierarchy and
multiple
inheritance of pure virtual classes aka interfaces in C#. Interfaces
provide the
same functionality as pure virtual classes in C++.

Regards,
Jeff
The problem here has to do with multiple inheritence. As you know, C#
only
supports single inheritence. Of course, you can implement multiple
interfaces. But, what does this really mean?

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #14
AFAIK, interface methods asre called through a vtable.
If they wouldn't, how would the compiler determine the address of the method
if I say test.Dispose() where test is of type IDisposible? Test could be any
class.
Under the hood, interface methods are declared as sealed *and* virtual.

"Frisky" <Fr***********@ NorthPole.Net> schrieb im Newsbeitrag
news:uI******** ******@TK2MSFTN GP09.phx.gbl...
Cody,

The problem is that the method is not declared as virtual in the third party class. Hence, the Dispose method is not virtual. (No VTable) Not the other
way around.

Frisky

"cody" <de********@gmx .de> wrote in message
news:uk******** ******@tk2msftn gp13.phx.gbl...
Since your implementation of IDisposable.Dis pose() overwrite the original entry of Dispose in the vtable there is no way to get back the pointer to the original Dispose method.
If you find no other solution for your problem you can try with
reflection.
<jo*@martinsoun d.com> schrieb im Newsbeitrag
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
How can I call a base interface method?

class ThirdPartyClass :IDisposable { //I can not modify this class
void IDisposable.Dis pose() {
Console.WriteLi ne( "ThirdPartyClas s Dispose" );
}
}

class MyClass :ThirdPartyClas s, IDisposable {
void IDisposable.Dis pose() {
Console.WriteLi ne( "MyClass Dispose" );
//base.IDisposabl e.Dispose(); //How can I do this !!!!
}
}



Nov 17 '05 #15
Cody,

Ok, you got me on a technicality. Internally, yeah, the compiler does some
wierd stuff to make things work.

And, I forgot to say that this was caused by the fact that the method is an
explicit interface method.

But, generically, the VTable is used to lookup virtual functions for a
class. Not an interface. (The "V" in VTable stands for virtual.)

Interfaces do not have virtual methods. Classes do. Interfaces do not have
implementations . Classes do.

In your example, you interchange interface and class; yet they are
distinctly different. (As shown above.)

Yes, when you implement a class, the interface methods in the class are
marked virtual, but final. Hence, you can "new" them, but not override them.
True virtual functions are those that may be overriden. The resaon the are
sealed (marked final) is because there is no inheritence. Unless of course
you implement them in a class, and then inherit from the class. But, then,
unless you mark the method virtual, you still can only new them.

However, in the example, the problem is that the method was declared as an
explicit interface. Internally this is seen as an override, but, it is an
override of an interface which has no implementation. Hence, you get no
inheritence. And, the compiler will not let you call the base method either,
because an interface does not have implementation.

As I pointed out in my other post, the method needs to be declared as a
regular class method to provide implementation inheritence. Since it is
defined explicitly as an interface (no implementation) , the compiler won't
let you call the (as far as it is concerned) non-existent implementation.
Hence, you must resort to hack (reflection) to get at the implementation.

"cody" <de********@gmx .de> wrote in message
news:uM******** ******@TK2MSFTN GP10.phx.gbl...
AFAIK, interface methods asre called through a vtable.
If they wouldn't, how would the compiler determine the address of the
method
if I say test.Dispose() where test is of type IDisposible? Test could be
any
class.
Under the hood, interface methods are declared as sealed *and* virtual.

"Frisky" <Fr***********@ NorthPole.Net> schrieb im Newsbeitrag
news:uI******** ******@TK2MSFTN GP09.phx.gbl...
Cody,

The problem is that the method is not declared as virtual in the third

party
class. Hence, the Dispose method is not virtual. (No VTable) Not the
other
way around.

Frisky

"cody" <de********@gmx .de> wrote in message
news:uk******** ******@tk2msftn gp13.phx.gbl...
> Since your implementation of IDisposable.Dis pose() overwrite the original > entry of Dispose in the vtable there is no way to get back the pointer to > the original Dispose method.
> If you find no other solution for your problem you can try with
> reflection.
>
>
> <jo*@martinsoun d.com> schrieb im Newsbeitrag
> news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
>> How can I call a base interface method?
>>
>> class ThirdPartyClass :IDisposable { //I can not modify this class
>> void IDisposable.Dis pose() {
>> Console.WriteLi ne( "ThirdPartyClas s Dispose" );
>> }
>> }
>>
>> class MyClass :ThirdPartyClas s, IDisposable {
>> void IDisposable.Dis pose() {
>> Console.WriteLi ne( "MyClass Dispose" );
>> //base.IDisposabl e.Dispose(); //How can I do this !!!!
>> }
>> }
>>
>
>



Nov 17 '05 #16

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

Similar topics

39
6552
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. When it completes, it can call a success, or a failure function. The names of these success, or failure functions will differ, and I'd like to know how I can pass the name of a function to my tool, and how my tool can call the function, using that...
9
11577
by: keith | last post by:
I created a class libery which has name space Assembly and class Assembly and compiled it. Then created a C# project and called a method in the external class e.g. Assembly dll; dll=Assembly.LoadFrom(@"c:\app\Assembly.dll");
24
7692
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my Dispose-Method and call the GC nothing happend!!! my Disposemethod has never been called!! so the GC dont call my Dispose-Method although I implemented IDisposable? what am i doing wrong?
4
2857
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. '**************************************************************************** ' Issues '****************************************************************************
2
1790
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...
9
5200
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
2
6121
by: Kevin Frey | last post by:
In a derived class I am trying to redefine the implementation of a interface method defined in a base class - the base class interface method was not declared virtual. I have yet to actually test this approach, but it looks ugly. Is there any better way? public interface ITest { void Func();
4
1946
by: archimed7592 | last post by:
Hi. for example i have base class A and dirved class B: struct A { virtual void f() { std::cout << "A::f()" << std::endl; } }; struct B : public A {
3
10263
by: Julie | last post by:
Here's the scenario (public attributes, etc. omitted for brevity): class Base { } class Derived : Base { }
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
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
9863
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...
1
7409
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
6673
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
5299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.