• 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()
                {
                }
            }
        }
    }
  • 相关阅读:
    数组常用方法
    Chrome调式技巧
    Object.create()和new object()和{}的区别
    ES6基础知识
    sass基础
    webpack中package.json相关参数
    webpack.config.js====图片处理
    java发送邮件功能[转]
    mybatis sql使用经验总结
    JSON操作
  • 原文地址:https://www.cnblogs.com/yangyunzhou/p/1968202.html
Copyright © 2020-2023  润新知