c# 装饰器模式专项——过滤器
- 装饰器模式专项——过滤器Filter
- 1.winform实现通过特性改控件名称(.Framework)
- 2.手写过滤器 (.NET Core)
装饰器模式专项——过滤器Filter
1.winform实现通过特性改控件名称(.Framework)
- FormFilterAttribute
public class FormFilterAttribute:Attribute
{public string FormText { get; set; }public int FormWidth { get; set; } = 100;public FormFilterAttribute(){}public void Executing(Form form){form.Text = FormText;form.Width = FormWidth;}
}
- RunWithAop(重新写启动函数)
public static class RunWithAop
{public static void RunAop(Form form){AopMethod(form);Application.Run(form);}private static void AopMethod(Form form){Type type = form.GetType();if (type.IsDefined(typeof(FormFilterAttribute), true)){FormFilterAttribute filter =(FormFilterAttribute)type.GetCustomAttributes(true).FirstOrDefault(m => m is FormFilterAttribute);filter.Executing(form);}}public static void ShowWithAop(this Form form){AopMethod(form);form.Show();}public static void ShowDialogWithAop(this Form form){AopMethod(form);form.ShowDialog();}
}
- 调用
[FormFilter(FormText="AAAA",FormWidth =2000)]
Form3 form3 = new();
form3.ShowDialog();
2.手写过滤器 (.NET Core)