c# 条件编译 Conditional ("DEBUG")
2012-02-24 17:45
423 查看
c# 条件编译 Conditional ("DEBUG")
简而言之:可以通过Conditional 指定函数和属性是否编译到最终产品中去。同时还应该看看 AttributeUsage Obsolete
C# Language Specification
(Section 17.4.1), which is used to describe the ways in which an attribute class can be used.
(Section 17.4.2), which is used to define conditional methods.
(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
The conditional method must have a return type of
The conditional method must not be marked with the
be explicitly marked with a
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:
//z 2012-2-24 17:47:38 PM is2120@csdn
简而言之:可以通过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
Conditionalattribute 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
overridemodifier. A conditional method may be marked with the
virtualmodifier, however. Overrides of such a method are implicitly conditional, and must not
be explicitly marked with a
Conditionalattribute.
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
相关文章推荐
- 使用条件属性 [Conditional("DEBUG")],而不使用#if DEBUG
- 改善C#程序的50种方法 条款4:使用Conditional特性代替#if条件编译
- C#条件编译选项:Conditional(代替#if...#endif)
- Debug 模式 和 Release 模式下,一个线程在"编译"上引发的问题
- c#断点无效:"当前不会命中断点。没有与此行关联的可执行代码。可能的原因包括:条件编译或编译器优化"
- C# ASP.NET调试问题<compilation debug="false" targetFramework="4.0"/>
- 使用codeblock 编译工程发出警告:"Debug" uses an invalid compiler. Skipping...
- C#条件编译选项:Conditional(代替#if...#endif)
- 处理编译错误"0" is an invalid value for the "DebugInformation" parameter of the "DCC" task.
- Tornado"debug=True"动态编译
- C# 使用Conditional特性而不是#if条件编译
- C#条件编译选项:Conditional(代替#if...#endif)
- C#条件编译选项:Conditional(代替#if...#endif)
- C# VS2010发布问题---<compilation debug="true" targetFramework="4.0">
- 编译时出现fatal error C1853: "debug/xxx.pch" is not a precomplied header file with......错误的原因及解决办法(转)
- 理解 Win32 "OutputDebugString"
- 编译"Objective-C基础教程" 工程时出错, SDK不匹配的解决办法
- Android源码编译时,遇到的问题:error: "_FORTIFY_SOURCE" redefined [-Werror]
- c# 条件编译
- "序列化" Serializable 理解 (asp.net C#)