views:

845

answers:

7

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. e.g., 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).

+2  A: 

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 */)
Mehrdad Afshari
I'm willing to use dirty tricks! It can't be *that* crazy if VB happily accepts it...well ok, it can be, but I'd like to see...
Michael Haren
Why is reflection dirty trick ? :) Its quite common nowdays and u can even make it perform equaly to code without reflection
majkinetor
It's considered a dirty trick when you gotta use polymorphism.
Mehrdad Afshari
@Micheal Haren: I converted it to VB for you. The last parameter will be the parameters passed to the method, if you need it.
Mehrdad Afshari
Heh, I thought it. Let's convert back then )
Mehrdad Afshari
@Michael Haren: Oh yes, thanks for pointing it out, I was thinking about getting the method first and so... Updating..
Mehrdad Afshari
A: 

Declare them as virtual:

http://msdn.microsoft.com/en-us/library/9fkccyh4(VS.71).aspx

Stu
Declare what as virtual? The methods aren't present in the base classes.
Michael Haren
The base class methods need to be virtual, along with the "override" keyword in the sub-class implementations. See my answer below.
Kevin
@Kevin: the methods aren't in the base class. I commented further on your answer.
Michael Haren
+2  A: 

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).

Lucero
He has no control over the classes.
Mehrdad Afshari
Maybe I need to be more specific then... ;)
Lucero
Right. This is what I started with but without control over the classes, I can't do it.
Michael Haren
@Lucero: Maybe you are right. Your solution works even if you have control over subclasses. I'm not sure of it though. Leave it here, at least to help future visitors.
Mehrdad Afshari
So I'd need to subclass each of the three classes (base, a, b) and add in an interface each time, e.g. MissingBaseMethods{ Add(); CreateNewItem();}; add a little NotImplemented code for the base, ignore A and B and then instantiate these new classes?
Michael Haren
If I got that mostly right then it might not be too bad. It'll avoid reflection and keep the method calls nice and tidy. I'll have to think about it. Thanks for the help!
Michael Haren
Since base is "abstract" (in the sense that it does not have those methods in the interface), you don't need to override this one. For the rest you're correct, you can then just do a cast to your interface and you're done.
Lucero
Ah, yes of course. Thanks Lucero!
Michael Haren
A: 

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 */);
   }
}
Scott Ivey
Can you show us some code samples? I doubt it's possible to do it cleanly.
Mehrdad Afshari
That's a nice idea but the whole point is to eliminate big switch blocks like this. Thanks, though.
Michael Haren
if you don't want the switch blocks - go with the reflection route. If performance is a concern, the switch block might be a better route. Having extension method lets you isolate that switch bock into an easily callable method.
Scott Ivey
as a 2nd thought, why not make an extension method that does reflection?
Scott Ivey
In general I couldn't say, but in this case it's because I'm not using VS2008. Thanks again.
Michael Haren
This isn't really a solution.
majkinetor
A: 

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.

Lucero
A: 

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.

Kevin
@Kevin, thanks for the help but what I was trying to show is that I can't touch that code--I don't control it. the //no present lines are actually NOT in the code file--if they were there I'd be golden.
Michael Haren
Ah, I see. So basically you're looking for interface-like behavior on things that don't declare a common interface. Kinda shoddy design if the things with common methods don't implement an interface. Too bad.
Kevin
A: 

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();
    }
}
MStodd
MStodd, thanks for the suggestion but unfortunately I don't have control over the classes. Additionally, those method calls aren't actually present in the base class, abstract or concrete.
Michael Haren