• 学习笔记--C#特性Attribute(一)


    这个框框好烦人啊,删不掉

    一、背景

    [serializable]
    public class Person(){}

    这是我第一次看到特性(Attribute),那时我还不知道这是什么,怎么会有这种写法,让我纠结了很久,我只知道,加了这个后,这个类就可以序列化了。

    二、使用

    定义自己的Attribute

    //自定义特性,跟普通的类差不多,继承Attribute,
    //这个类中有2个构造函数,和一个属性
    public class MyClassAttribute:Attribute
    {
        public MyClassAttribute(){}
        public MyClassAttribute(string text)
        {
             MyProperties=text;
             Console.WriteLine("你输入的参数为:"+text);
        }
        public string MyProperties{get;set;}
    }
    
    //这里定义了两个特性,用与后面测试
    public class MyMethodAttribute:Attribute
    {
        public MyMethodAttribute(){}
        public MyMethodAttribute(string text)
        {
             MyProperties=text;
             Console.WriteLine("你输入的参数为:"+text);
        }
        public string MyProperties{get;set;}
    }

    ②如何使用特性:

    //特性使用很简单,就是在想使用的地方用一个个中括号引用就可以了
    [TestClassAttribute("特性使用在类上")]
    public class Test
    {
            [TestMethodAttribute("特性使用在方法上")]
            public void TestAccess()
         {
          Console.WriteLine("这是一个测试方法");
         } }

    ③测试

    class Program
    {
         static void Main(string[] args)
        {
             Test testData=new Test();
             TestData.TestAccess();
        }
    } 

    输出结果:
    这是一个测试方法

    结论:
    附加
    特性的类或方法,在运行时不会受特性影响(如果该特性没做什么事)
    它只是在类或方法上多加了一段提示信息,类似于一段注释代码,不过用IL反汇编程序(开始菜单中搜索IL),可以看出,特性被编译到dll中,而注释是没有这个待遇的

    ④获取特性的值

    public class GetValueFromMyAttribute
    {
        public static void Run() 
        {
            bool isDefinedMethodAttribute = Attribute.IsDefined(typeof(Test).GetMethod("TestAccess"), typeof(MyMethodAttribute));
            bool isDefinedClassAttribute = Attribute.IsDefined(typeof(Test), typeof(MyClassAttribute));
    
            if (isDefinedMethodAttribute)
            {
              MyMethodAttribute attr = Attribute.GetCustomAttribute(typeof(Test).GetMethod("TestAccess"), typeof(MyMethodAttribute)) as MyMethodAttribute;
    
              Console.WriteLine(attr.MyProperties); 
         }

         if (isDefinedClassAttribute)
         {
          TestClassAttribute attr
    = Attribute.GetCustomAttribute(typeof(Test), typeof(MyClassAttribute)) as MyClassAttribute;

          Console.WriteLine(attr.
    MyProperties);
         }
      }
    }

    ⑤测试

    class Program
    {
         static void Main(string[] args)
        {
             Test testData=new Test();
             TestData.TestAccess();
         GetValueFromMyAttribute.Run();
      }
    }

    输出结果:
    这是一个测试方法
    你输入的参数为:
    特性使用在方法上(构造函数)
    特性使用在方法上(获取到的值)
    你输入的参数为:特性使用在类上
    特性使用在类上

    三、系统中常用的Attribute

    除了一开始出现Serializable、后来还遇到过HttpPost、HttpGet、AjaxPro.AjaxMethod()等等

    DllImport:导入第三方dll

    class InCommonUse
    {
    [DllImport("User32.dll")]
    public static extern in MessageBox(int hParent,string message,string caption,int type);
    
    public static void Test()
    {
     MessageBox(0,"外部导入了User32.dll文件","提示",0)
     Console.Readkey();
    }
    }

    Conditional:指定模式下运行

    [Conditional("DEBUG")]
    private
    static void TestConditional() {
      Console.WriteLine("只在debug模式下运行");
    }

    Obsolete:过时提示

    这个估计大部分人都遇到过

    这次就写到这里了,等待后续学习

    四、总结


    文章总得有个结尾,前几天考四级,学到了一句,

    According what has been discussed above, this is a good thing, hereby, write a blog to record this important moment

  • 相关阅读:
    填空练习(指向指针的指针)
    练习指针函数:编写一个函数,输入n为偶数时,调用fa函数,当输入n为奇数时,调用fb函数(利用指针函数)。
    输入一个整数,并将其反转后输出。
    有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
    案例练习
    操作don树
    Node对象
    element对象二
    element对象
    在末尾添加节点
  • 原文地址:https://www.cnblogs.com/Ruan/p/3482821.html
Copyright © 2020-2023  润新知