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()
                {
                }
            }
        }
    }
  • 相关阅读:
    归并排序
    CTE 递归
    Cordova 入门文档
    Javascript 原型链
    Windows11 正式版偷渡开启安卓子系统
    快速解决gerrit merge confict问题
    利用VPS来搭建个人主页
    检测串行序列10010
    Verilog语法总结
    深度学习中常见优化算法学习笔记
  • 原文地址:https://www.cnblogs.com/yangyunzhou/p/1968202.html
Copyright © 2011-2022 走看看