第11章特性,程序集

1,提醒使用新方法

class Wolf
    {
        [Obsolete("该方法已过时,推荐使用:NewDrawMySelf()方法",false)]
        public void DrawMySelf()
        {
            Console.WriteLine(@"
            *     *    *     /\__/\  *    ---    *
               *            /      \    /     \   
                    *   *  |  -  -  |  |       |*  
             *   __________| \     /|  |       |   
               /              \ T / |   \     /   
             /                      |  *  ---
            |  ||     |    |       /             *
            |  ||    /______\     / |*     *
            |  | \  |  /     \   /  |
             \/   | |\ \      | | \ \
                  | | \ \     | |  \ \
                  | |  \ \    | |   \ \
                  '''   '''   '''    '''   ");
        }
        public void NewDrawMySelf()
        {
            Console.WriteLine(@"");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Wolf wolf = new Wolf();
            wolf.DrawMySelf();
            //wolf.NewDrawMySelf();
            Console.ReadKey();
        }
    }

2,使用特性修饰自定义特性

 class AnimalInfoAttribute : Attribute
    {
        public AnimalInfoAttribute(String name)
        {
            this.name = name;
        }

        public  string name;
        private string phylum;
        public string Phylum
        {
            get { return phylum; }
            set { phylum = value; }
        }
        private String classis;
        public String Classis
        {
            get { return classis; }
            set { classis = value; }
        }
        private String family;
        public String Family
        {
            get { return family; }
            set { family = value; }
        }
    }
 
    [AnimalInfo("狼", Family = "犬科", Phylum = "脊索动物门", Classis = "哺乳纲")]
    class Wolf
    {
       
    }
    class Program
    {
        static void Main(string[] args)
        {
            //获取附着在Wolf类上的特性信息
            Attribute attr =
                Attribute.GetCustomAttribute(typeof(Wolf), typeof(AnimalInfoAttribute));

            AnimalInfoAttribute aia = attr as AnimalInfoAttribute;

            if (aia == null)
            {
                Console.WriteLine("Wolf类不存在特性...");
            }
            else
            {
                Console.WriteLine(aia.name + ":");
                Console.WriteLine("门:" + aia.Phylum);
                Console.WriteLine("纲:" + aia.Classis);
                Console.WriteLine("科:" + aia.Family);
            }
            Console.ReadKey();
        }
    }

总结:

特性是被用来修饰程序元素如:方法,变量,属性,类,接口,结构体等等;

自定义的特性本质也是一个类,创建自定义特性需要集成:System.Attribute类。

程序集(Assembly)是组织程序的逻辑单元,我们编写好的代码最终会被编译器编译为若干个程序集。

两种常见的程序集:可执行文件(。exe);动态链接文件(。dll)。

 

转载于:https://www.cnblogs.com/xyb1117/archive/2012/02/24/2366844.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值