.Net Framework的aop
/// <summary> /// 特性类 /// </summary> public class FormFilterAttribute:Attribute { private readonly string _formTitle; public FormFilterAttribute(string formTitle) { this._formTitle = formTitle; } /// <summary> /// 通过特性设置标题 /// </summary> /// <param name="form"></param> public void SetTitle(Form form) { form.Text = _formTitle; } }
/// <summary> /// 调用特性的类 /// </summary> public class RunWithAop { /// <summary> /// aop通用的方法 /// </summary> /// <param name="form"></param> public static void AopMethod(Form form) { /** * 1.获取特性 * 2.得到特性的实例,调用特性中的方法 */ Type type= form.GetType(); if (type.IsDefined(typeof(FormFilterAttribute),true)) { //调用GetCustomAttribute方法需要引用反射的命名空间。否则只能看到GetCustomAttributes方法。 FormFilterAttribute attribute = (FormFilterAttribute)type.GetCustomAttribute(typeof(FormFilterAttribute)); attribute.SetTitle(form); } } /// <summary> /// 给外界调用的方法 /// </summary> /// <param name="form"></param> public static void RunApp(Form form) { AopMethod(form); Application.Run(form); } public static void Show(Form form) { AopMethod(form); form.Show(); } public static void ShowDialog(Form form) { AopMethod(form); form.ShowDialog(); } }
static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //Application.Run(new Form1()); RunWithAop.RunApp(new Form1()); } }