Using TypeBuilder to create a pass-through constructor for the base class

Viewed 5606

Say I have a SpaceShip class, like so:

public class SpaceShip {
    public SpaceShip() {  }
    public SpaceShip(IRocketFuelSource fuelSource) {  }
}

I want to use TypeBuilder to create a type at run-time which inherits from SpaceShip, and defines one constructor for each of the ones in SpaceShip. I don't need the constructors to actually do anything except pass their arguments up to the parent ("pass-through" constructors). For example, the generated type would look something like this if expressed in C#:

public class SpaceShipSubClass : SpaceShip {
    public SpaceShipSubClass() : base() {  }
    public SpaceShipSubClass(IRocketFuelSource fuelSource) : base(fuelSource) {  }
}

To complicate things a bit, I don't actually know which class the generated type will be inheriting from until run-time (so I'll have to take into account any number of constructors, possibly with default parameters).

Is this possible? I think I could figure it out if I had a general direction to start with, it's just that I'm completely new to TypeBuilder.

Thanks!

1 Answers
Related