Explicit interface implementation from abstract class

K

KH

Hi, I can't seem to find the right syntax to do this (if it's even
possible), so if you know and would share I'd appreciate it!

With an interface like ISerializable I like to do an explicit implementation
because its one method, GetObjectData(), isn't meaningful for anything but
serialization, so I just don't want to see it in intellisense unless the
object is being treated as an ISerializable.

Anyways I ran into the situation (shown in code below) where I am inheriting
from an abstract class which implements ISerializable, but I can't call
base.GetObjectData() from an inheritor because I can't treat base as an
ISerializable.

If I don't do an explicit implementation in the base class then everything
works of course, but I'd like to do the explicit implementation if possible,
not to mention just being curious what the syntax is if it exists.

I've included my syntax guesses and the errors they produce. THanks in
advance for any info!

- KH


using global::System;
using global::System.Runtime.Serialization;

[Serializable]
public abstract class A : ISerializable
{
// virtual // The modifier 'virtual' is not valid for this item
void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext ctx)
{
}
};

[Serializable]
public class C : A, ISerializable
{
void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext ctx)
{
// base.GetObjectData(info, ctx); // 'A' does not contain a
definition for 'GetObjectData'
// (base as ISerializable).GetObjectData(info, ctx); // Use of
keyword 'base' is not valid in this context
}
};
 
P

Pavel Minaev

Hi,  I can't seem to find the right syntax to do this (if it's even
possible), so if you know and would share I'd appreciate it!

With an interface like ISerializable I like to do an explicit implementation
because its one method, GetObjectData(), isn't meaningful for anything but
serialization, so I just don't want to see it in intellisense unless the
object is being treated as an ISerializable.

Anyways I ran into the situation (shown in code below) where I am inheriting
from an abstract class which implements ISerializable, but I can't call
base.GetObjectData() from an inheritor because I can't treat base as an
ISerializable.

If I don't do an explicit implementation in the base class then everything
works of course, but I'd like to do the explicit implementation if possible,
not to mention just being curious what the syntax is if it exists.

I've included my syntax guesses and the errors they produce. THanks in
advance for any info!

- KH

using global::System;
using global::System.Runtime.Serialization;

[Serializable]
public abstract class A : ISerializable
{
    // virtual // The modifier 'virtual' is not valid for this item
    void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext ctx)
    {
    }

};

[Serializable]
public class C : A, ISerializable
{
    void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext ctx)
    {
        // base.GetObjectData(info, ctx); // 'A' does not containa
definition for 'GetObjectData'
        // (base as ISerializable).GetObjectData(info, ctx); // Use of
keyword 'base' is not valid in this context
    }
};

There's no way to access an explicit interface implementation of the
method in base class from a derived class. Technically, this is the
case because the explicit implementation itself is private, and thus
not accessible directly - only via the vtable slot. And that gets
overwritten when you re-implement that method...
 
K

KH

Interesting - thanks Pavel.


Pavel Minaev said:
Hi, I can't seem to find the right syntax to do this (if it's even
possible), so if you know and would share I'd appreciate it!

With an interface like ISerializable I like to do an explicit implementation
because its one method, GetObjectData(), isn't meaningful for anything but
serialization, so I just don't want to see it in intellisense unless the
object is being treated as an ISerializable.

Anyways I ran into the situation (shown in code below) where I am inheriting
from an abstract class which implements ISerializable, but I can't call
base.GetObjectData() from an inheritor because I can't treat base as an
ISerializable.

If I don't do an explicit implementation in the base class then everything
works of course, but I'd like to do the explicit implementation if possible,
not to mention just being curious what the syntax is if it exists.

I've included my syntax guesses and the errors they produce. THanks in
advance for any info!

- KH

using global::System;
using global::System.Runtime.Serialization;

[Serializable]
public abstract class A : ISerializable
{
// virtual // The modifier 'virtual' is not valid for this item
void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext ctx)
{
}

};

[Serializable]
public class C : A, ISerializable
{
void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext ctx)
{
// base.GetObjectData(info, ctx); // 'A' does not contain a
definition for 'GetObjectData'
// (base as ISerializable).GetObjectData(info, ctx); // Use of
keyword 'base' is not valid in this context
}
};

There's no way to access an explicit interface implementation of the
method in base class from a derived class. Technically, this is the
case because the explicit implementation itself is private, and thus
not accessible directly - only via the vtable slot. And that gets
overwritten when you re-implement that method...
 

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