• Effective C# 学习笔记(四)使用Conditional Attributes 替代 #if


    用法

    [Conditional("DEBUG"),

    Conditional("TRACE")]

    private void CheckState()

    {

    // Grab the name of the calling routine:

    string methodName =

    new StackTrace().GetFrame(1).GetMethod().Name;

    Trace.WriteLine("Entering CheckState for Person:");

    Trace.Write("\tcalled by ");

    Trace.WriteLine(methodName);

    Debug.Assert(lastName != null,

    methodName,

    "Last Name cannot be null");

    Debug.Assert(lastName.Length > 0,

    methodName,

    "Last Name cannot be blank");

    Debug.Assert(firstName != null,

    methodName,

    "First Name cannot be null");

    Debug.Assert(firstName.Length > 0,

    methodName,

    "First Name cannot be blank");

    Trace.WriteLine("Exiting CheckState for Person");

     

    }

    private void CheckStateBad()

    {

    // The Old way:

    #if BOTH

    Trace.WriteLine("Entering CheckState for Person");

    // Grab the name of the calling routine:

    string methodName =

    new StackTrace().GetFrame(1).GetMethod().Name;

    Debug.Assert(lastName != null,

    methodName,

    "Last Name cannot be null");

    Debug.Assert(lastName.Length > 0,

    methodName,

    "Last Name cannot be blank");

     

    Debug.Assert(firstName != null,

    methodName,

    "First Name cannot be null");

    Debug.Assert(firstName.Length > 0,

    methodName,

    "First Name cannot be blank");

    Trace.WriteLine("Exiting CheckState for Person");

    #endif

    }

     

    定义组合变量

    #if ( VAR1 && VAR2 )

    #define BOTH

    #endif

  • 相关阅读:
    Selenium学习之==>常见面试题
    Selenium学习之==>Switch与SelectApi接口详解
    Selenium学习之==>ActionChainsApi接口详解
    Selenium学习之==>WebDriverApi接口详解
    Selenium学习之==>三种等待方式
    Selenium学习之==>18种定位方式的使用
    avaScript 的基础学习(一)
    前端基础之CSS
    Http协议
    前端基础之html
  • 原文地址:https://www.cnblogs.com/haokaibo/p/2096529.html
Copyright © 2020-2023  润新知