是否可以在属性上分配属性并使用它来分配其他属性 – 这样做而不使用反射?
代码:
public class CashierOut : BaseActivity { [Description("Flag indicates whether break to execution.")] [DefaultValue(false)] [MyCustomAttribute(ParameterGroups.Extended)] public bool CancelExecution { get; set; } [Description("Flag indicates whether allow exit before declation.")] [DefaultValue(true)] [MyCustomAttribute(ParameterGroups.Extended)] [DisplayName("Exit before declaration?")] public bool AllowExitBeforeDeclare { get; set; } }
我想做这样的事情:
public class CashierOut : BaseActivity { [MyResourceCustom("CashierOut.CancelExecution")] public bool CancelExecution { get; set; } [MyResourceCustom("CashierOut.AllowExitBeforeDeclare")] public bool AllowExitBeforeDeclare { get; set; } } public sealed class MyResourceCustom : Attribute { public string ResourcePath { get; private set; } public ParameterGroupAttribute(string resourcePath) { ResourcePath = resourcePath; // Get attributes attributes value from external resource using the path. } }
解决方法
属性只是将元数据添加到他们定义的成员 – 他们自己什么都不做.
您必须使用反射才能根据属性值产生一些行为.
这就是所有属性的工作原理 – 一些工具知道某些属性(如编译器和ConditionalAttribute),但这仍然是通过反射完成的.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。