views:

154

answers:

1

Assuming I've got several unrelated classes in separate files with a main method in each. Currently project is a console application and I just change a startup object in IDE to run respective main which instantiates respective class from IDE. Out of curiosity would it be possible to change the startup object dynamically at runtime somehow before launching this exe as an alternative of having this project as a class library and repeating code which I have in main(s) somewhere else.

+2  A: 

I don't think this is possible. It's been quite some time since I dived into the details, but if I remember correctly, the specification of which "Main" to run gets written into the IL. It's not something you get to change at runtime.

The traditional solution to this problem is to make your "Main" method parse the command line arguments and then run the appropriate method. I'm sure you can minimize, or even eliminate, repeating code by refactoring correctly.

Ragesh