using System;
using System.Diagnostics;
namespace attribute实验
{
class conditional
{
[Conditional("Dotrance")]
internal static void TraceMessage(string str)
{
Console.WriteLine(str);
}
///不可行返回值必须为Void
//[Conditional("Dotrance")]
//internal static bool testBool(string str)
//{
// Console.WriteLine(str);
// return true;
//}
}
}
#define Dotrance
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace attribute实验
{
class Program
{
static void Main(string[] args)
{
PrintOut("start of Main");
TraceMessage("测试打印所在文件夹");
double nome = TrstDebuggerStepThrough(1, 2);
}
[Obsolete("use method SuperPrintOut")]//过时方法的调用
static void PrintOut(string str)
{
Console.WriteLine(str);
conditional.TraceMessage("测试是否调用");
//bool i = conditional.testBool("测试bool返回值");
}
public static void TraceMessage(string message,
[CallerMemberName] string memberName = null,//打印调用方面名称
[CallerFilePath] string sourceFilePath = null,
[CallerLineNumber] int sourceLineNumber = default(int))
{
Debug.WriteLine("message: " + message);//右击项目文件——属性——生成——配置——活动(Release)——勾选“定义 Debug 常量”。 输出|显示输出来源|调试
Debug.WriteLine("member name: " + memberName);
Debug.WriteLine("source file path: " + sourceFilePath);
Debug.WriteLine("source line number: " + sourceLineNumber);
Trace.WriteLine("message: " + message);
Trace.WriteLine("member name: " + memberName);
Trace.WriteLine("source file path: " + sourceFilePath);
Trace.WriteLine("source line number: " + sourceLineNumber);
Console.WriteLine("message: " + message);
Console.WriteLine("member name: " + memberName);
Console.WriteLine("source file path: " + sourceFilePath);
Console.WriteLine("source line number: " + sourceLineNumber);
}
[DebuggerStepThrough]//代码调试直接跳过
public static double TrstDebuggerStepThrough(int x,int y)
{
double norm = Math.Sqrt(x * x + y * y);
return norm;
}
}
}
上文使用的了4种常用的特性
Obsolete
这个预定义特性标记了不应被使用的程序实体。它可以让您通知编译器丢弃某个特定的目标元素。例如,当一个新方法被用在一个类中,但是您仍然想要保持类中的旧方法,您可以通过显示一个应该使用新方法,而不是旧方法的消息,来把它标记为 obsolete(过时的)。
Conditional
这个预定义特性标记了一个条件方法,其执行依赖于指定的预处理标识符。
它会引起方法调用的条件编译,取决于指定的值。
这里需要注意的是这个特性标记的方法返回值必须为void,否则报错。
CallerMemberName&CallerFilePath&CallerLineNumber
这三条是标记函数被调用的名称,文件位置和行号,通常是用来查找错误错误的。
https://blog.csdn.net/y13156556538/article/details/70153477
http://www.cnblogs.com/TianFang/archive/2012/08/19/2646146.html
DebuggerStepThrough
定义调试时不进入调试器。当你使用F11进行dubug工作时,经常会进入到一些不想进入的property或method内部。
其他的特性
有比较多.NET还在不断的充实,用到哪些学哪些,比如
自定义特性
// 描述如何使用一个自定义特性 SomethingAttribute
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
//********自定义特性SomethingAttribute**************//
public class SomethingAttribute : Attribute {
private string name; // 名字
private string data; // 日期
public string Name {
get { return name; }
set { name = value; }
}
public string Data {
get { return data; }
set { data = value; }
}
public SomethingAttribute(string name) {
this.name = name;
this.name = name;
}
}
最少要定义一个公共构造函数,否则会保错。
AttributeUsage
预定义特性 AttributeUsage 描述了如何使用一个自定义特性类。它规定了特性可应用到的项目的类型。
特性类的特性。
使用特性
[Something("Amy", Data = "2018-06-18")]
[Something("Jack", Data = "2018-06-18")]
class Test{}
特性的用途
Type t = typeof(Test);
var something = t.GetCustomAttributes(typeof(SomethingAttribute),true);
foreach(SomethingAttribute each in something)
{
Console.WriteLine("Name:{0}", each.Name);
Console.WriteLine("Data:{0}",each.Data);
}
最常用的函数就是GetCustomAttributes(),这个函数可以将制定对象的制定特性中定义的字段提取出来。