替代条件编译在C# [英] Alternatives to Conditional Compilation in C#

查看:127
本文介绍了替代条件编译在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是替代其在C#中使用条件编译的代码?

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

我有了很多的代码的类,它基于的#ifdef ..一段时间后我的代码不可读。

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

寻找重构技术用于更好的可读性和维修的许多代码#如果 DEFS

Looking for refactoring techniques to use for better readability and maintenance of code with many #if defs

推荐答案

有一件事是使用的 ConditionalAttribute

One thing is to use the ConditionalAttribute:

[Conditional("DEBUG")]
public void Foo()
{
    // Stuff
}

// This call will only be compiled into the code if the DEBUG symbol is defined
Foo();



这还是条件编译,但基于属性而非 #IFDEF ,这使得它一般简单。

It's still conditional compilation, but based on attributes rather than #ifdef, which makes it generally simpler.

另一种方法是简单地在执行时使用,而不是做这一切在编译时的布尔值。如果你可以给我们你想要什么来实现,你是如何使用条件编译的更多详细信息,这将是有益的。

Another alternative is simply to use Boolean values at execution time, instead of doing it all at compile time. If you could give us more details of what you're trying to achieve and how you're using conditional compilation, that would be useful.

这篇关于替代条件编译在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆