zoukankan      html  css  js  c++  java
  • Attribute 实例

    下面会弹2个对话框
    A方法是特别的X方法
    B方法是普通的方法

     using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Reflection;
    
    namespace WindowsFormsApplication14
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                foreach (MethodInfo MI in this.GetType().GetMethods())
                {
                    bool Special = false;
    
                    foreach (Attribute Attrib in MI.GetCustomAttributes(true))
                        if (MI.Name == "A")
                            if (Attrib is XAttribute)
                            {
                                Special = true;
                                break;
                            }
                    if (Special)
                        MessageBox.Show(MI.Name + "方法是特别的X方法");
                    else if (MI.Name == "B") // 因为其他普通的方法太多,这里就显示B
                        MessageBox.Show(MI.Name + "方法是普通的方法");
                }
            }
    
            [X]
            public void A()
            {
            }
    
            public void B()
            {
            }
    
            [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
            public class XAttribute : Attribute
            {
                public XAttribute()
                {
                }
            }
        }
    }
  • 相关阅读:
    Java ee第七周作业
    Java ee第六周作业
    Java ee第五周作业
    Java ee第四周作业
    Java ee第三周作业
    第二周作业-web后台应用开发与xml
    Java ee第一周的作业
    在团队项目中我对自己的总结
    两人合作,黄金点游戏
    c语言实现wc功能
  • 原文地址:https://www.cnblogs.com/yangyunzhou/p/1968202.html
Copyright © 2011-2022 走看看