下面会弹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() { } } } }