简体   繁体   English

C#precompile指令-使用变量来告诉要编译的代码

[英]C# precompile directive - use a variable to tell which code to compile

I use a web service in a C# project. 我在C#项目中使用Web服务。 I test the project both online and on localhost. 我同时在线和在本地测试该项目。 I have code like: 我有这样的代码:

 //var proxy = new PanoNestWebsite.ScheduleImagePanonest.ScheduleImageProcessingClient();
 //var s = new ScheduleImagePanonest.ScheduleImageProcessing();
 var proxy = new ScheduleImageLocal.ScheduleImageProcessingClient();
 var s = new ScheduleImageLocal.ScheduleImageProcessing();

For example, If I want to deploy it online I uncomment the first two lines, and comment the last two. 例如,如果要在线部署它,请取消注释前两行,并注释后两行。

But I have much more changes to make, and I don't like commenting and uncommenting all lines. 但是我还有很多要做的改变,而且我不喜欢注释和取消注释所有行。 How can I use proprocessing directives to define a variable like: IsLocal and I just set it to true to include the local code, or to false to include the server code? 如何使用预处理指令来定义一个变量,例如:IsLocal并将其设置为true以包括本地代码,或设置为false以包括服务器代码? How can I write something like this? 我该如何写这样的东西?

If this is the kind of thing that you're dealing with, I'd recommend an alternative approach, possibly some IOC/DI where both of these classes implement an interface, and you configure in your app.config/web.config which type to point to it. 如果这是您要处理的事情,我建议您使用另一种方法,可能是一些IOC / DI,其中这两个类都实现了接口,并在app.config / web.config中配置哪种类型指出这一点。

So instead of 所以代替

var proxy = new ScheduleImageLocal.ScheduleImageProcessingClient();

you'd instead do 你宁愿做

var proxy = IoCFactory.Resolve<IScheduleImagingProcessingClient>();

or something to that effect, however your library works. 或类似的东西,但是您的库可以工作。

If you use Unity, you would point the IScheduleImagingProcessingClient interface to a concrete type in your config file. 如果使用Unity,则将IScheduleImagingProcessingClient接口指向配置文件中的具体类型。

You can use the project properties to define values or #define SOME_KEY then: 您可以使用项目属性定义值或#define SOME_KEY,然后:

#if KEY

    ... Code goes here

#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM