473,756 Members | 1,823 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 #1
15 12789
<jo*@martinsoun d.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 !!!!
}
}


Good question - and to be honest, I don't *think* you can...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2


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 !!!!
}
}


Hi,

Usually when a base class implements an IDisposable, it also implements
a "protected virtual void Dispose(bool disposing)" where the acctually
cleanup is done... (if it is done correctly).
and the Dispose() method calls it.

try to override it as follow:

class ThirdPartyClass :IDisposable
{ //I can not modify this class
void IDisposable.Dis pose()
{
GC.SuppressFina lize(this);
Dispose(false);
}

protected virtual void Dispose(bool disposing)
{
Console.WriteLi ne( "ThirdPartyClas s Dispose" );
}
}

class MyClass :ThirdPartyClas s, IDisposable
{
protected override void Dispose(bool disposing)
{
// Do your cleanup here
Console.WriteLi ne( "MyClass Dispose" );
base.Dispose(di sposing);
}
}

if it doesn't implement a Dispose(bool disposing)

then you should do as you did, only:

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.Dispose(); // just call the base class implementation of the
Dispose.
}
}

Cheers,
Eyal.

Nov 17 '05 #3


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 !!!!
}
}


Hi,

Usually when a base class implements an IDisposable, it also implements

a "protected virtual void Dispose(bool disposing)" where the acctually
cleanup is done... (if it is done correctly).
and the Dispose() method calls it.

try to override it as follow:

class ThirdPartyClass :IDisposable
{ //I can not modify this class
void IDisposable.Dis pose()
{
GC.SuppressFina lize(this);
Dispose(false);
}

protected virtual void Dispose(bool disposing)
{
Console.WriteLi ne( "ThirdPartyClas s Dispose" );
}

}

class MyClass :ThirdPartyClas s, IDisposable
{
protected override void Dispose(bool disposing)
{
// Do your cleanup here
Console.WriteLi ne( "MyClass Dispose" );
base.Dispose(di sposing);
}
}
if it doesn't implement a Dispose(bool disposing) and it implements the
IDisposable as: void IDisposable.Dis pose(), then I guess you are
screwed... :(

Cheers,
Eyal.

Nov 17 '05 #4
Jon
My real problem involves a much more complicated class that uses
IBindingList.
I just used IDisposed in the posting as a simple example.

Like you say, I guess I am screwed.

Nov 17 '05 #5


Jon wrote:
My real problem involves a much more complicated class that uses
IBindingList.
I just used IDisposed in the posting as a simple example.

Like you say, I guess I am screwed.


HEY!!!! you are not screwed!!!!

I found you the answer!!!!

class MyClass :ThirdPartyClas s, IDisposable
{
void IDisposable.Dis pose()
{
// Do your cleanup here
Console.WriteLi ne( "MyClass Dispose" );

// Call the base.IDisposabl e.Dispose()
Type baseType = typeof(ThirdPar tyClass);
InterfaceMappin g map =
baseType.GetInt erfaceMap(typeo f(IDisposable)) ;
map.TargetMetho ds[0].Invoke(this, new object[]{});
}
}
I just knew IDisposable has only one method, so I used
map.TargetMetho ds[0], you'll have to find your method in the
collection...

Good luck..

Eyal.

Nov 17 '05 #6


Eyal Safran wrote:
Jon wrote:
My real problem involves a much more complicated class that uses
IBindingList.
I just used IDisposed in the posting as a simple example.

Like you say, I guess I am screwed.


HEY!!!! you are not screwed!!!!

I found you the answer!!!!


or even more generic:

class MyClass :ThirdPartyClas s, IDisposable
{
void IDisposable.Dis pose()
{
// Do your cleanup here
Console.WriteLi ne( "MyClass Dispose" );

// Call the base.IDisposabl e.Dispose()
InterfaceMappin g map =
GetType().BaseT ype.GetInterfac eMap(typeof(IDi sposable));
map.TargetMetho ds[0].Invoke(this, new object[]{});
}
}

Eyal.

Nov 17 '05 #7
Jon,

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?

If you think about it, it means you can only inherit one implementation.
From this persepective, the IDisposable interface cannot have
implementation. By definition, it is an interface, not an implementation.
Hence, in your derived class, you can have the Dispose() method. But, you
don't get to inherit any base class functionality.

Now to the Crux of it... But ThirdPartyClass is a class, so why can't I
inherit its functionality. And the reason is, because the Dispose method in
this class is explicitly defined as an interface only. If the base class had
been defined as:

class ThirdPartyClass : IDisposable
{ //I can not modify this class
public void Dispose()
{
Console.WriteLi ne("ThirdPartyC lass Dispose");
}
}

or better yet:

class ThirdPartyClass : IDisposable
{ //I can not modify this class
public virtual void Dispose()
{
Console.WriteLi ne("ThirdPartyC lass Dispose");
}
}

then you would be able to inherit from this member. In the former case you
could use the "new" keyword, and in the later case "override" to implement
your method. For example, in the former case:

public override void Dispose()
{
Console.WriteLi ne("MyClass Dispose");
base.Dispose(); // now this works
}

or in the later case:

public new void Dispose()
{
Console.WriteLi ne("MyClass Dispose");
base.Dispose(); // now this works
}

You can use some trickery to get at the base type and get its hidden
implementation. But the purpose of the explicit interface definition was to
hide it and not allow this.

The trickery involves using reflection to get to the actual implementation
of the base class, and force the hidden method invocation. For example, as
suggested by Eyal Safrin:

public virtual void Dispose()
{
Console.WriteLi ne("MyClass Dispose");
InterfaceMappin g map =
base.GetType(). BaseType.GetInt erfaceMap(typeo f(IDisposable)) ;
map.TargetMetho ds[0].Invoke(this, new object[] {});
}

To fix this third party library once and for all, you might then create the
following wrapper class:

class ThirdPartyWrapp er : ThirdPartyClass
{
public virtual void Dispose()
{
Console.WriteLi ne("Wrapper class");
InterfaceMappin g map =
base.GetType(). BaseType.GetInt erfaceMap(typeo f(IDisposable)) ;
map.TargetMetho ds[0].Invoke(this, new object[] {});
}
}

class MyClass : ThirdPartyWrapp er, IDisposable
{
public override void Dispose()
{
Console.WriteLi ne("MyClass Dispose");
base.Dispose();
}
}

Then use this class for all internal usage.

Hope this helps...

--
Frisky

Intellectuals solve problems; geniuses prevent them. ~ Albert Einstein
<jo*@martinsoun d.com> wrote in message
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 #8
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 #9
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 #10

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

Similar topics

39
6550
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
11576
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
7691
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
2856
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
1789
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
6118
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
1945
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
9873
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...
0
9713
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
8713
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
7248
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
5142
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...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3806
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
2
3359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
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.