简体   繁体   中英

How do I call a subclass method on a baseclass object?

I'm using a library that generates a bunch of classes for me.

These classes all inherit from a common base class but that base class doesn't define a couple methods that are common to all subclasses.

For example:

SubClassA : BaseClass{
  void Add(ItemA item) {...}
  ItemA CreateNewItem() {...}
}

SubClassB: BaseClass{
  void Add(ItemB item) {...}
  ItemB CreateNewItem() {...}
}

Unfortunately, the base class doesn't have these methods . This would be great:

BaseClass{
  // these aren't actually here, I'm just showing what's missing:
  abstract void Add(ItemBaseClass item);  // not present!
  abstract ItemBaseClass CreateNewItem(); // not present!
}

Since there is a common base class for my A+B objects and a common base class for the Item objects, I had hoped to benefit from the wonderful world of polymorphism.

Unfortunately, since the common methods aren't actually present in the base class, I can't call them virtually. eg, this would be perfect:

BaseClass Obj;
Obj = GetWorkUnit(); // could be SubClassA or SubClassB

ItemBaseClass Item = Obj.CreateNewItem(); // Compile Fail: CreateNewItem() isn't in the base class

Item.DoSomething();

Obj.Add(Item); // Compile Fail: Add(...) isn't in the base class

Obviously casting would work but then I'd need to know which type I had which would negate the benefits.

How can I "force" a call to these methods? I'm not worried about getting an object that doesn't implement the method I'm trying to call. I can actually do what I want in VB--I don't get intellisense but the compiler's happy and it works:

CType(Obj, Object).Add(Item) // Note: I'm using C#--NOT VB

Againt, I have no control over these classes (which I think rules out partial classes).

You cannot call a non-virtual method of a derived class without resorting to reflection or other dirty tricks. If you want to do it, it's easy then:

// obj is your object reference.
obj.GetType().InvokeMember("MethodName", 
    System.Reflection.BindingFlags.InvokeMethod, null, obj, null /* args */)

I might be missing something, but why not make and inherit from an interface with those methods?

If you are in control of the creation process for the instances, you might get what you want by inheriting from the classes which you didn't write and add then interface (no code needs to be written, the compiler will map the interface to the existing non-virtual methods).

If you're using Visual Studio 2008 - you can create an extension method for the base class. Inside that extension method, you'd do the cast and call the subclass's method. This should work for 2.0 framework targeted projects as well, as long as you're compiling from withing VS 2008. Borrowing the other suggested reflection code, you could do the following...

public static class MyExtensions
{
   public static ItemBaseClass CreateNewItem(this BaseClass item)
   {
      return item.GetType().InvokeMember("MethodName", System.Reflection.BindingFlags.InvokeMethod, null, obj, null /* args */);
   }
}

Another possibility is to use some proxy mechanism, such as a RealProxy (not very efficient in terms of performance though) or better by using dynamically created DynamicMethods so that the overhead of using reflection is only there once per type to support, while keeping it as flexible as the reflection method. This pays off big when you need to call the method several times, but it requires some MSIL knowledge.

Problem #1: your subclasses aren't overriding anything Problem #2: your subclasses have different function signatures than your base class

Make sure your signatures are correct, and if you're overriding them, mark them virtual and not abstract. You'll have to add an empty body to the base class virtual functions if you don't want them to do anything.

class ItemBase{}

class ItemA : ItemBase{}

class ItemB : ItemBase{}

class BaseClass
{
    public virtual void Add(ItemBase item){}
    public virtual ItemBase CreateItem() { return null; }
}

class ClassA : BaseClass
{
    public override void Add(ItemBase item)
    {
        //do whatever
        throw new NotImplementedException();
    }

    public ItemBase CreateItem()
    {
        //create an ItemBase and return
        throw new NotImplementedException();
    }
}

You forgot the "override" keyword in your sub-classes' method definitions. When something's declared "abstract" it is by definition virtual, and thus in the sub-class you need to put the "override" keyword in front of the method declaration. So what should work is below:

BaseClass{
  abstract void Add(ItemBaseClass item);  // not present!
  abstract ItemBaseClass CreateNewItem(); // not present!
}

SubClassA : BaseClass{
  override void Add(ItemA item) {...}
  override ItemA CreateNewItem() {...}
}

SubClassB: BaseClass{
  override void Add(ItemB item) {...}
  override ItemB CreateNewItem() {...}
}

This should work exactly as you want in your usage example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM