Alternatives to Conditional Compilation in C#

前端 未结 7 2446
感情败类
感情败类 2021-02-07 21:17

What is the alternative to having code with conditional compilation in C#?

I have a class that has lots of code that is based on # ifdef .. After sometime my code is unr

7条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-07 21:48

    Polymorphism.

    Approach it the same way as you would any spaghetti code with lots of conditional branches on the same condition.

    Abstract out the differences into a base class or interface.

    Construct the concrete class depending on build (one #if). Pass the concrete object to your App, and then your app calls the methods defined on the interface.

提交回复
热议问题