我可以通过添加子类来解决添加功能的问题然后为什么我应该使用装饰模式什么是装饰模式的真正优势?
//Get PropertyDescriptor object for the given property name
var propDesc = TypeDescriptor.GetProperties(typeof(T))[propName];
//Get FillAttributes methodinfo delegate
var methodInfo = propDesc.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public |
BindingFlags.NonPublic)
.FirstOrDefault(m => m.IsFamily || m.IsPublic && m.Name == "FillAttributes");
//Create Validation attribute
var attribute = new RequiredAttribute();
var attributes= new ValidationAttribute[]{attribute};
//Invoke FillAttribute method
methodInfo.Invoke(propDesc, new object[] { attributes });
Run Code Online (Sandbox Code Playgroud)
您好我正在尝试使用上面的代码在运行时添加Validation属性.但是我得到以下例外:
收集是固定的大小
用 CSS 编写时,我可以添加一类“徽章”并获得我想要的。按钮或选项卡附近的一个小数字,带有一些样式,以表明此控件具有需要审查的待处理信息。
甚至有一个帖子在这里,在有人试图做我想做的iOS上的
我想在 WinForms 上的按钮或选项卡上执行此操作。如果 WPF 中有更简单的解决方案,那么我可能会考虑使用它。
这是一张图像,显示了我想要实现的目标:
