【问题标题】:Monotouch Compiler Directive if debug调试时的 Monotouch 编译器指令
【发布时间】:2010-11-23 02:23:55
【问题描述】:

在我的 MonoTouch 应用程序中,我怎样才能放入 # 编译器指令以仅在调试模式下包含代码?

【问题讨论】:

    标签: c# xamarin.ios compiler-directives


    【解决方案1】:

    MonoDevelop 在创建解决方案时默认设置 DEBUG 定义,因此您可以使用两件事:您可以在用于检测代码的方法上使用 [Conditional ("DEBUG")] 属性,并且可以使用标准 if #DEBUGs 在您的源代码中。

    像这样:

    [Conditional ("DEBUG")]
    void Log (string msg)
    {
        Console.WriteLine (msg);
    }
    
    void Foo ()
    {
        Log ("Start");
        ..
        Log ("End");
    }
    

    Conditional 属性的好处在于,如果未设置开关,编译器将在编译时删除调用,这比在源代码中乱扔:

    #if DEBUG
    Console.WriteLine ("start");
    #endif
    

    【讨论】:

    • 可以把这个Log的定义贴在我的命名空间的顶部吗?还是必须是类上的方法?
    猜你喜欢
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    相关资源
    最近更新 更多