0

I've implemented ISerializable in both a child class and a parent class, like this:

class CircuitElement : ISerializable
{
    ...
    protected void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
    {
        if (info == null)
            throw new ArgumentNullException("info");
        info.AddValue("ID", ID);
    }
}

class Bus : CircuitElement, ISerializable
{
    ...
    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
    {
        if (info == null)
            throw new ArgumentNullException("info");
        ((ISerializable)base).GetObjectData(info,context);
        info.AddValue("Voltage", Voltage);
        info.AddValue("BaseVoltage", BaseVoltage);
        info.AddValue("Location", Location);
    }
}

But in the child class Bus, I'm getting the error Use of keyword base is not valid in this context. I know I can just implement the interface implicitly on the parent CircuitElement class, and then I don't have to worry about the conversion, but was under the impression that explicit implementation is more appropriate for this scenario (for reasons akin to those presented here: https://stackoverflow.com/a/143425/996592)

Is there a way to do the conversion, or am I stuck implementing the ISerializable interface in the parent class implicitly?

Community
  • 1
  • 1
Fabian Tamp
  • 4,416
  • 2
  • 26
  • 42
  • 1
    This is already invalid: `protected void ISerializable.GetObjectData` - when you use explicit interface implementation, you don't specify an access modifier. And then you *can't* call that implementation from a re-implementation. – Jon Skeet Nov 22 '12 at 07:11
  • Yep, sorry. copy-paste mistake on that one. – Fabian Tamp Nov 22 '12 at 10:22

2 Answers2

2

Personally, I disagree with the perspective presented in the answer you linked to. Explicit interface implementation is simply more problematic, for exactly the reason you've given: it really doesn't play nicely with inheritance.

If you're reimplementing an interface which has been implemented explicitly, there's just no way of calling that implementation on an object of a type which reimplements the same interface, including from within the code of that reimplementation.

Two options:

  • Use implicit interface implementation instead. Personally I usually find this simpler. I only use explicit interface implementation when the interface has members which don't really make sense when you're thinking about the class itself.

  • Continue to use explicit interface implementation, but make that implementation just call a protected virtual method. You can then override that virtual method in the derived class and call the base implementation.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

You don't need to convert using "base" keyword. Actually you can't use "base" keyword in this context. You should simply call the base method if you wanna include the parent information using "base.GetObjectData" if you implement the "ISerializable" interface in the implicit way and it will call the parent method with owner of caller instance.

This behavior is one of the simple things in the Object-Oriented model and you can't change that. On the other hand you can use "virtual" keyword and try overriding the parent method (in this case I don't think that you need this method).

I think you should take a look at here on stackoverflow, there are some great answers about the ways of implementing interfaces.

Also there is a perfect manual about Polymorphism on MSDN here. It will help you to understand why you can'n convert "base" and you will find some technics to do that in the other specified ways.

My Idea:

Using "dynamic" keyword and "ExpandoObject" is a better idea for creating a dynamic model and do serialization things on it. In my personal opinion it's much more faster in many cases, also it's so simple to implement.

Hope it help.

Community
  • 1
  • 1
Rikki
  • 3,338
  • 1
  • 22
  • 34