C# Attribute 19年

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(),这个函数可以将制定对象的制定特性中定义的字段提取出来。

最后就是要扩展阅读,特性这个东西还是要用,才能体会到其妙处!
添加链接描述
添加链接描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值