Effective C# 学习笔记(四)使用Conditional Attributes 替代 #if
2011-07-02 23:14 小郝(Kaibo Hao) 阅读(501) 评论(0) 编辑 收藏 举报用法
[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
出处:http://www.cnblogs.com/haokaibo/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。