加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#属性:统治它们的一个属性?

发布时间:2020-12-15 08:20:12 所属栏目:百科 来源:网络整理
导读:是否可以在属性上分配属性并使用它来分配其他属性 – 这样做而不使用反射? 代码: public class CashierOut : BaseActivity{ [Description("Flag indicates whether break to execution.")] [DefaultValue(false)] [MyCustomAttribute(ParameterGroups.Exte
是否可以在属性上分配属性并使用它来分配其他属性 – 这样做而不使用反射?

代码:

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),但这仍然是通过反射完成的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读