zoukankan      html  css  js  c++  java
  • 通过声明Attribute属性改变不同类的输出效果

    ConsoleApplication--控制台应用程序

    首先创建基类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Attribute_Exercise
    {
        /// <summary>
        /// 此处的注释不影响编译和运行,只是给开发者提供帮助
        /// </summary>  
        //[some Attribute]  e.g.
        [Obsolete] //[Obsolete]、[Obsolete("")]、[Obsolete("",true/false)]  Obsolete的构造函数 F12查看 
        [Serializable]
        public class BasicClass
        {
            //基类属性、字段
            public int ID { set; get; }
    
            public string Name { set; get; }
    
        }
                
        public class AClass:BasicClass
        {
        
        }
    
        public class BClass : BasicClass
        { 
        
        }
    
        public class CClass : BasicClass
        { 
        
        }
    }
    View Code

    其次自定义一个Attribute:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Attribute_Exercise
    {
        /// <summary>
        /// 自定义一个Attribute:somename+Attribute
        /// 使用时:[somename]
        /// </summary>
        public class CustomizedAttribute:Attribute
        {
            //以下是构造函数
            public CustomizedAttribute()
            { 
               //
            }
    
            public CustomizedAttribute(string msg)
            { 
            
            }
    
            public CustomizedAttribute(string msg, bool err)
            { 
                
            }
    
            //字段
            public string Msg;
            public bool Err;
    
                   
    
    
    
        }
    
        public class esleCusomizedAttribute : Attribute
        { 
        
        }
    }
    View Code

    在主入口中:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Attribute_Exercise
    {
        class Program
        {
            static void Main(string[] args)
            {
                //主要运用了继承的机制
                BasicClass bc = new BasicClass() { ID=1,Name="基类1"};
                Console.WriteLine("基类ID:{0}  ;基类类名:{1}",bc.ID,bc.Name);
                Console.ReadLine();
            }
        }
    }
    View Code

    先看看基类的效果:

    下面进入主题--如何在不改变基类及其继承类的对象的情况下,仅仅通过声明Attribute,使得这些继承类具有不同的行为或者效果?

    1.声明Attribute:在BasicClass.cs Line 23处增加 [Customized] ;

    2.增加相应Attribute的方法:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;//反射
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Attribute_Exercise
    {
        public class AttributeActivities
        {
            public static void OutPut<T>(T t) where T : BasicClass
            {
                Type type=t.GetType();
                Attribute attribute = type.GetCustomAttribute(typeof(CustomizedAttribute));//注意  using System.Reflection;
                if (attribute != null && attribute is CustomizedAttribute)
                {
                    CustomizedAttribute customizedAttr = attribute as CustomizedAttribute;
    
                    Console.WriteLine("当前类额外的Attribute:{0}",
                        customizedAttr.ToString().Substring(customizedAttr.ToString().IndexOf(".")+1));
                    Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++");
                }
                
            }
        }
    }
    View Code

    3.在程序主入口调用方法:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Attribute_Exercise
    {
        class Program
        {
            static void Main(string[] args)
            {
                //主要运用了继承的机制
                BasicClass bc = new BasicClass() { ID=1,Name="基类1"};
                Console.WriteLine("基类ID:{0}  ;基类类名:{1}",bc.ID,bc.Name);
                Console.WriteLine("*************************************************");
                AttributeActivities.OutPut<AClass>(new AClass()
                {
                    ID=2,
                    Name="AClass"                
                });
                Console.WriteLine("*************************************************");
                AttributeActivities.OutPut<BClass>(new BClass()
                {
                    ID = 3,
                    Name = "BClass"
                });
                Console.WriteLine("*************************************************");
                AttributeActivities.OutPut<CClass>(new CClass()
                {
                    ID = 4,
                    Name = "CClass"
                });
    
                Console.ReadLine();
            }
        }
    }
    View Code

    结果截图:

    是不是感觉很神奇呢? 并没有对这个继承类做任何修改,只是多了个声明,然后附加相应的方法就实现了拥有不同Attribute的类具有不同的效果。

    读者也可以自己扩展其它的方法试试哦。。。

    补充:

    Attribute里增加:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Attribute_Exercise
    {
        /// <summary>
        /// 自定义一个Attribute:somename+Attribute
        /// 使用时:[somename]
        /// </summary>
        public class CustomizedAttribute:Attribute
        {
            //以下是构造函数
            public CustomizedAttribute()
            { 
               //
            }
    
            public CustomizedAttribute(string msg)
            {
                
            }
    
            public CustomizedAttribute(string msg, bool err)
            { 
                
            }
    
            //字段
            public string Msg;
            public bool Err;
    
            //增加部分
            //扩展方法
            public int Number;
            public void DoSomeThing(string msg)
            {
                if (Err)
                {
                    Console.WriteLine("{0}--{1}",msg,DateTime.Now);
                }
            }
            public CustomizedAttribute(string msg, int number, bool err) 
            {
                this.Msg = msg;
                this.Number = number;
                this.Err = err;
            }
    
    
    
    
                   
        }
    
        public class esleCusomizedAttribute : Attribute
        { 
        
        }
    }
    View Code

    方法调用:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;//反射
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Attribute_Exercise
    {
        public class AttributeActivities
        {
            public static void OutPut<T>(T t) where T : BasicClass
            {
                Type type=t.GetType();
                Attribute attribute = type.GetCustomAttribute(typeof(CustomizedAttribute));//注意  using System.Reflection;
                if (attribute != null && attribute is CustomizedAttribute)
                {
                    CustomizedAttribute customizedAttr = attribute as CustomizedAttribute;
    
                    Console.WriteLine("当前类额外的Attribute:{0}",
                        customizedAttr.ToString().Substring(customizedAttr.ToString().IndexOf(".")+1));
                    Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++");
                    //调用扩展方法
                    customizedAttr.DoSomeThing(t.Name);
                }
                
            }
        }
    }
    View Code

    在继承类前声明Attribute:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Attribute_Exercise
    {
        /// <summary>
        /// 此处的注释不影响编译和运行,只是给开发者提供帮助
        /// </summary>  
        //[some Attribute]  e.g.
        [Obsolete] //[Obsolete]、[Obsolete("")]、[Obsolete("",true/false)]  Obsolete的构造函数 F12查看 
        [Serializable]   
        public class BasicClass
        {
            //基类属性、字段
            public int ID { set; get; }
           
            public string Name { set; get; }
    
        }
         //声明Attribute
        [Customized]   
        public class AClass:BasicClass
        {
        
        }
        //声明扩展Attribute的构造函数
        [Customized("",1,true)]
        public class BClass : BasicClass
        { 
        
        }
    
        public class CClass : BasicClass
        { 
        
        }
    }
    View Code

    结果:

    注:未经作者同意,禁止转载!转载请说明出处!请尊重知识产权。

  • 相关阅读:
    省选测试13
    省选测试12
    省选测试11
    省选测试9
    省选测试10
    省选测试8
    省选测试7
    省选测试6
    倍增 LCA && ST表
    博客园markdown
  • 原文地址:https://www.cnblogs.com/5696-an/p/5638312.html
Copyright © 2011-2022 走看看