开发者

Changing startup object of console application at runtime

开发者 https://www.devze.com 2022-12-23 13:16 出处:网络
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 IDEto run res

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

精彩评论

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

关注公众号