zoukankan      html  css  js  c++  java
  • C#:特性

    #define IsText//添加一个宏,接触注释
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _002特性
    {
        [MyText("张三",Id =20)]
        class Program
        {
            static void Main(string[] args)
            {
                Type type = typeof(Program);
                object[] obj = type.GetCustomAttributes(false);
                foreach (var item in obj)
                {
                    Console.WriteLine(item);
                    Console.WriteLine(((MyTextAttribute)item).Name);
                    Console.WriteLine(((MyTextAttribute)item).Id);
                }
               // Text1();
                //Text2();特性Obsolete为true程序就不能使用了
                //Text3();
                //Text4("张三");
            }
            [Obsolete("这个程序还可以用一段时间")]
            public static void Text1()
            {
                Console.WriteLine("Text1");
            }
            [Obsolete("该程序作废,禁止使用",true)]
            public static void Text2()
            {
                Console.WriteLine("Text2");
            }
           [Conditional("IsText")]//该方法被注释掉了(字符串作为标记进行注释)
            public static void Text3()
            {
                Console.WriteLine("Text3");
            }
            public static void Text4(string name,[CallerFilePath]string filepath="",[CallerLineNumber]int num=10,[CallerMemberName]string filename="")
            {
                Console.WriteLine("Text4");
                Console.WriteLine(name+filepath+num+filename);
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _002特性
    {
        /*1.特性类尾缀加上Attribute
         2.必须引用特性[AttributeUsage]并继承于System.Attribute
         3.特性内一般只有属性,没有方法
         4.构造函数内可以添加参数
             */
        [AttributeUsage(AttributeTargets.Class)]
        class MyTextAttribute:System.Attribute
        {
            private string name;
            private int id;
    
            public string Name { get => name; set => name = value; }
            public int Id { get => id; set => id = value; }
    
            public MyTextAttribute(string name)
            {
                this.Name = name;
            }
        }
    }
    莫说我穷的叮当响,大袖揽清风。 莫讥我困时无处眠,天地做床被。 莫笑我渴时无美酒,江湖来做壶。
  • 相关阅读:
    元组,字典
    for循环补充,变量和不可变量,数字类型,字符串类型,列表类型
    流程控制之while循环,for循环
    运算符,流程控制之if判断
    变量,常量,基本数据类型、运算符
    蓝桥杯--算法提高 排列数 (简单dfs)
    蓝桥杯-- 历届试题 核桃的数量 (gcd)
    hdoj--1272--小希的迷宫(并查集)
    zzulioj--1769--去师院的旅程:能怎么走(三)(0.0)
    zzulioj--1638--Happy Thanksgiving Day
  • 原文地址:https://www.cnblogs.com/huang--wei/p/10062632.html
Copyright © 2011-2022 走看看