c# 条件编译 Conditional (DEBUG)
c# 条件编译 Conditional ("DEBUG")
简而言之:可以通过Conditional 指定函数和属性是否编译到最终产品中去。同时还应该看看 AttributeUsage Obsolete
C# Language Specification
System.AttributeUsageAttribute
(Section 17.4.1), which is used to describe the ways in which an attribute class can be used.System.Diagnostics.ConditionalAttribute
(Section 17.4.2), which is used to define conditional methods.System.ObsoleteAttribute
(Section 17.4.3), which is used to mark a member as obsolete.
//z 2012-2-24 17:47:38 PM is2120@csdn
利用 Conditional 属性,程序员可以定义条件方法。Conditional 属性通过测试条件编译符号来确定适用的条件。当运行到一个条件方法调用时,是否执行该调用,要根据出现该
调用时是否已定义了此符号来确定。如果定义了此符号,则执行该调用;否则省略该调用(包括对调用的参数的计算)。
条件方法要受到以下限制:
条件方法必须是类声明或结构声明中的方法。如果在接口声明中的方法上指定 Conditional 属性,将出现编译时错误。
条件方法必须具有 void 返回类型。
不能用 override 修饰符标记条件方法。但是,可以用 virtual 修饰符标记条件方法。此类方法的重写方法隐含为有条件的方法,而且不能用 Conditional 属性显式标记。
条件方法不能是接口方法的实现。否则将发生编译时错误。
A conditional method is subject to the following restrictions:
- The conditional method must be a method in a class or struct declaration. A compile-time error occurs if the
Conditional
attribute is specified on a method in an interface declaration. - The conditional method must have a return type of
void
. - The conditional method must not be marked with the
override
modifier. A conditional method may be marked with thevirtual
modifier, however. Overrides of such a method are implicitly conditional, and must not be explicitly marked with aConditional
attribute. - The conditional method must not be an implementation of an interface method. Otherwise, a compile-time error occurs.
In addition, a compile-time error occurs if a conditional method is used in a delegate-creation-expression.
//z 2012-2-24 17:47:38 PM is2120@csdn
示例1:
// CondMethod.cs
// compile with: /target:library /d:DEBUG
using System;
using System.Diagnostics;
namespace TraceFunctions
{
public class Trace
{
[Conditional("DEBUG")]
public static void Message(string traceMessage)
{
Console.WriteLine("[TRACE] - " + traceMessage);
}
}
}
示例2:
using
System;
using System.Diagnostics;
|
public class Trace
|
{
|
[Conditional( "DEBUG" )]
|
public Static void ShowMessage( string msg)
|
{
|
Console.WriteLine( " [Trace - " + msg);
|
}
|
}
|
public class Program
|
{
|
public static void Main()
|
{
|
Trace.ShowMessage( "Starting" );
|
Console.WriteLine( "运行中" );
|
Trace.ShowMessage( "Ending" );
|
}
|
}
//z 2012-2-24 17:47:38 PM is2120@csdn
转载于:https://www.cnblogs.com/IS2120/archive/2012/02/24/6745939.html
c# 条件编译 Conditional (DEBUG)相关推荐
- Java 条件编译 Conditional Compilation
根据Java编译器的优化的机制,Java也能够提供条件编译.对于条件设为false的语句,编译器将不对条件覆盖的代码段生成字节码. 不仅可以用简单的boolean常量值来做条件编译,还可以用字符串常量 ...
- Qt 通过条件编译区分Debug和Release代码
Qt 通过条件编译区分Debug和Release代码 在公司写Bug,发现程序运行全屏不适合调试,通过条件编译搞一下. 文章目录 Qt 通过条件编译区分Debug和Release代码 测试代码 关 ...
- 在MSBuild.exe中使用条件编译(Conditional Compile)
参数的写法:"/p:DefineConstants=MY_PREPROC_FLAG YET_ANOTHER_FLAG" 注意,在使用 MSBuild 时,会出现C#项目中设置的输出 ...
- #if DEBUG vs. Conditional(“DEBUG”)
From StackOverflow: http://stackoverflow.com/questions/3788605/if-debug-vs-conditionaldebug From MSD ...
- C# 使用Conditional特性而不是#if条件编译
概述 #if/#endif 语句常用来基于同一份源码生成不同的编译结果,其中最常见的就是debug版和release版.但是这些工具在实际应用中并不是非常友好,因为它们容易被滥用,其代码页进而难以理解 ...
- C# DEBUG 调试信息打印及输出详解
转载自: http://blog.csdn.net/aaaaatiger/article/details/5583301 1.debug只在[debug模式下才执行](运行按钮后面的下拉框可选) 2. ...
- elif是不是java关键字_C# 中的#if、#elif、#else、#endif等条件编译符号 (转载)
这些是C#中的条件编译符号.这些指令我在项目中遇到过,查过网络,问过人(当然,既不认识大牛,也不认识小牛,所以没什么收获).今天翻看一本资料,有提到这个方面的东西,所以写下来和能看到这篇文章的人一起学 ...
- C# 中利用 Conditional 定义条件方法
利用 Conditional 属性,程序员可以定义条件方法.Conditional 属性通过测试条件编译符号来确定适用的条件.当运行到一个条件方法调用时,是否执行该调用,要根据出现该调用时是否已定义了 ...
- VS.Net中程序集的Debug版本和Release版本的区别
作者:未知 请作者速与本人联系 前几天看到豆腐的文章介绍如何知道程序集是Debug版还是Release版,之前只知道某些软件从功能上有企业版.标准版之分,却从不知道.Net程序集还有Debug和R ...
- #if DEBUG与条件(“ DEBUG”)
在大型项目中,哪种方法更好,为什么使用: #if DEBUGpublic void SetPrivateValue(int value){ ... } #endif 要么 [System.Diagno ...
最新文章
- JS组件系列——BootstrapTable 行内编辑解决方案:x-editable
- 移动端重构系列1——前期准备工作
- 放上了一篇几个月前写的东西
- java抽象类 抽象方法_Java基础 -- Java 抽象类 抽象方法
- 关于js中namespace命名空间模式
- mysql中int(16)_MySQL中int(M)和tinyint(M)数值类型中M值的意义
- 【152】C# 操作 Excel 杂记
- Bootstrap开启模态框后对数据处理(标记模态框的开启与关闭状态)
- python list转dict_python中将list转为dict
- java中使用es的dsl_基于DSL的基于图论的Java程序中输入图数据的方法
- html上传文件_.NET基于WebUploader大文件分片上传、断网续传、秒传
- countif函数比较两列不同_VLOOKUP函数批量查找,这么长的公式你可以写出来,立马加薪...
- 1039. Course List for Student (25)
- python简单爬虫代码-python爬虫超简单攻略,带你写入门级的爬虫,抓取上万条信息...
- 损失函数、tensorflow2实现——Python实战
- python 随机种子 时间_python课堂整理24----时间和随机数模块
- 九九乘法表c语言带表头,C语言-九九乘法表
- 计算机管理员永久访问权限,三种方法让你获得Win7管理员最高权限
- layui 弹出层 点击遮罩层关闭
- IDEA快速换行快捷键