Quote:Original post by TheUnbeliever
Quote:Original post by ChaosEngine
Why would you use reflection? What's wrong with either a generic approach or a polymorphic Create method?
Er, because I'm an idiot.
Lol, I wouldn't say that at all. Your solution will certainly work and is probably the most flexible approach. There might be an easier way, that's all.
To the OP, can you post a complete example of what you want. Don't just post 1 or 2 lines, show the entire function and an example of calling it.
Why do you want to pass in an instance of the type?
You've been given 3 solutions already, do any of them fit?
You could also do
Bullet CreateBulletOfSameType<BulletSubType>(BulletSubType bullet) where BulletSubType : Bullet, new(){ return new BulletSubType();}
this will work if you have
PistolAmmoNormal bullet = null;Bullet bulletCurrent = CreateBulletOfSameType(bullet);// bulletCurrent is a PistolAmmoNormal
but not
Bullet bullet = new PistolAmmoNormal;Bullet bulletCurrent = CreateBulletOfSameType(bullet);// bulletCurrent is a Bullet
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight