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!
Alright, I couldn't find anything online, so I ended up implementing my own. This should help start off anyone writing some sort of proxy, too.
Usage (assuming you have a
TypeBuilder
object -- see here for an example):