gpt4 book ai didi

C# 属性 : One Attribute to Rule Them All?

转载 作者:太空狗 更新时间:2023-10-29 21:18:59 24 4
gpt4 key购买 nike

是否可以在属性上分配一个属性并使用它来分配其他属性 - 在不使用反射的情况下这样做?

代码:

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

关于C# 属性 : One Attribute to Rule Them All?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8984967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com