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()
                {
                }
            }
        }
    }
  • 相关阅读:
    ProjectEuler 13
    ProjectEuler 8
    ProjectEuler 5
    ProjectEuler 6
    ProjectEuler 7
    ProjectEuler 9
    日程管理系统维护改善1
    日程管理系统改错
    android作业Text
    四则运算
  • 原文地址:https://www.cnblogs.com/yangyunzhou/p/1968202.html
Copyright © 2011-2022 走看看