Abe*_*bel 1 .net c# f# debug-build
我知道你可以#if DEBUG在C#中使用和喜欢,但是有可能创建一个完全被忽略的方法或类,包括没有包含在#if DEBUG块中的所有用法吗?
就像是:
[DebugOnlyAttribute]
public void PushDebugInfo()
{
// do something
Console.WriteLine("world");
}
Run Code Online (Sandbox Code Playgroud)
然后:
void Main()
{
Console.WriteLine("hello ");
Xxx.PushDebugInfo();
}
Run Code Online (Sandbox Code Playgroud)
其中,如果DEBUG定义将打印"hello world",否则只打印"hello".但更重要的是,MSIL在发布版本中根本不应包含方法调用.
我相信我所遵循的行为类似于Debug.WriteLine,其调用被完全删除,并且对发布版本中的性能或堆栈深度没有影响.
并且,如果可能在C#中,使用此方法的任何.NET语言都会表现相同(即,编译时与运行时优化).
还标记了f#,因为基本上我会在那里需要这个方法.
好像你在寻找ConditionalAttribute.
例如,这是Debug类源代码的一部分:
static partial class Debug
{
private static readonly object s_ForLock = new Object();
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert(bool condition)
{
Assert(condition, string.Empty, string.Empty);
}
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert(bool condition, string message)
{
Assert(condition, message, string.Empty);
}
................................
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
525 次 |
| 最近记录: |