开发者

Changing startup object of console application at runtime

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 s开发者_运维问答tartup 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜