为什么在创建属性时要使用 AttributeUsage AllowMultiple?

发布于 2024-08-17 13:01:31 字数 164 浏览 6 评论 0原文

根据我正在阅读的一本书,AttributeUsageAllowMultiple 公共属性指定:

...目标是否可以应用该属性的多个实例。

为什么我想要/不想使用这个?

According to a book I'm reading, the AllowMultiple public property of AttributeUsage specifies:

...whether the target can have multiple instances of the attribute applied to it.

Why would I want/not want to use this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ヤ经典坏疍 2024-08-24 13:01:31

属性是元数据。通常,您需要使用属性来修饰成员或类型,以便跟踪有关它的一些信息。

例如,PropertyGrid 使用 DescriptionAttribute 来标记属性的描述:

[Description("This is my property")]
public int MyProperty { get; set; }

大多数时候,拥有多个描述是没有意义的。

但是,某个特定属性可能需要多次使用才有意义。在这种情况下,您需要将属性设置为允许将其自身的多个实例标记为同一属性。

(并不是说我会这样做,但是......)假设您创建了一个自定义属性来跟踪对类的主要更改。您可能希望为每个重大更改列出此内容:

[Changes(Version=1.1, Change="Added Foo Feature")]
[Changes(Version=2.0, Change="Added Bar Feature")]
public class MyClass
{
    // ...

Attributes are meta-data. Typically, you'll want to decorate a member or type with an Attribute in order to track some information about it.

For example, the DescriptionAttribute is used by the PropertyGrid to label a description of a property:

[Description("This is my property")]
public int MyProperty { get; set; }

Most of the time, having more than one description would not make sense.

However, it is possible that a specific attribute makes sense to use more than once. In that case, you'd want to set the Attribute to allow multiple instances of itself tagged to the same attribute.

(Not that I'd do this, but...) Say you made a custom attribute to track major changes to a class. You might want to list this for every major change:

[Changes(Version=1.1, Change="Added Foo Feature")]
[Changes(Version=2.0, Change="Added Bar Feature")]
public class MyClass
{
    // ...
养猫人 2024-08-24 13:01:31

这个例子可能有点做作,但希望它能表达要点。

[Convertable(typeof(Int32)), Convertable(typeof(Double))]
public class Test
{

}

This example might be a little contrived but hopefully it gets the point across.

[Convertable(typeof(Int32)), Convertable(typeof(Double))]
public class Test
{

}
画▽骨i 2024-08-24 13:01:31

这取决于属性是什么。

例如,您可以创建一个属性来将类标记为依赖于某些内容,并且可以允许多个依赖项。

有关具体示例,请查看 SuppressMessage< /code>,它会抑制代码分析警告。一个成员可以有多个您可能想要抑制的警告。

另一个示例是 WebResource< /a>;一个程序集可以包含多个资源。

This depends what the attributes are.

For example, you could make an attribute that marks a class as depending on something, and you could allow multiple dependencies.

For a concrete example, look at SuppressMessage, which suppresses a code analysis warning. A member can have multiple warnings that you might want to suppress.

Another example is WebResource; an assembly can contain multiple resources.

多情出卖 2024-08-24 13:01:31

这里没有人为的例子,我在实际的生产代码中使用了它。我编写了一些代码来解析包含数据对(例如(代码=值))的文件。我在函数上放置了一个自定义属性,以指示应该为给定的代码调用它。

[CanParseCode("G1")]
[CanParseCode("G2")]
private void ParseGXCodes(string code, string value)
{
   ...
}

这种特殊的格式有点旧,并且是特定领域的,有数百种不同的代码。我的目标是编写一个框架,以便更轻松地编写文件处理器,该处理器可以仅提取所需的代码并忽略其余代码。多次允许相同的属性可以通过简单地在处理每个代码的函数上声明属性来轻松表达代码的意图。

No contrived example here, I used it in real production code. I wrote some code to parse a file containing pairs of data like (code=value). I put a custom attribute on a function to indicate it should be called for a given code.

[CanParseCode("G1")]
[CanParseCode("G2")]
private void ParseGXCodes(string code, string value)
{
   ...
}

This particular format is a somewhat old and domain specific with hundreds of different codes. My goal was to write a framework to make it easier to write file processors that could extract only the codes it needs and ignore the rest. Allowing the same attribute multiple times made it easy to express the intent of the code by simply declaring attributes on the function(s) that process each code.

转瞬即逝 2024-08-24 13:01:31

属性AllowMultiple=true 实用性的实际应用

[ManagesType(typeof(SPEC_SEC_OC), true)]
[ManagesType(typeof(SPEC_SEC_04_OC), true)]
public class LibSpecSelectionView : CustomView
{
    public LibSpecSelectionView(SPEC_SEC_OC)
    {}
    public LibSpecSelectionView(SPEC_SEC_O4_OC)
    {}
    ....
}

public static class ViewManager
{
   ...  static Dictionary of views built via reflection
   public void LaunchView(this CollectionBaseClass cbc)
   {
       ... Find class registered to handle cbc type in dictionary and call ctor
   }
}

SPEC_SEC_OC myOC = DataClient.Instance.GetSPEC_SEC_OC();
myOC.LaunchView()

今天早些时候我翻转了AllowMultiple=true,以允许多次使用ManagesType 属性。我们有数百个自定义集合类。大多数自定义集合都有一个继承自 CustomView 的视图,旨在处理特定类型的自定义集合的 UI 视图的创建并将其呈现给用户。 ManagesType 属性通过反射用于构建应用程序中每个视图的字典,该字典继承自 CustomView 以“注册”它旨在处理的对象类型。 LibSpecSelectionView 通过同时显示两个不同的集合(创建两个选项卡并在一个选项卡中显示一个自定义集合,在第二个选项卡中显示另一个)“打破了这种模式”,因此同一视图能够处理两个不同的自定义集合。
然后,通过扩展方法利用哪些视图能够处理哪些集合类型的字典,以允许我们的任何自定义集合通过一行启动注册视图(如果没有“注册”视图,则启动默认视图)调用视图管理器。

Real World Application of Attribute AllowMultiple=true usefulness

[ManagesType(typeof(SPEC_SEC_OC), true)]
[ManagesType(typeof(SPEC_SEC_04_OC), true)]
public class LibSpecSelectionView : CustomView
{
    public LibSpecSelectionView(SPEC_SEC_OC)
    {}
    public LibSpecSelectionView(SPEC_SEC_O4_OC)
    {}
    ....
}

public static class ViewManager
{
   ...  static Dictionary of views built via reflection
   public void LaunchView(this CollectionBaseClass cbc)
   {
       ... Find class registered to handle cbc type in dictionary and call ctor
   }
}

SPEC_SEC_OC myOC = DataClient.Instance.GetSPEC_SEC_OC();
myOC.LaunchView()

I flipped AllowMultiple=true earlier today to allow for the ManagesType attribute to be used more than once. We have several hundred Custom Collection Classes. Most of these custom collections have a view that inherits from CustomView designed to handle creation of a UI view for a specific type of custom collection and presenting it to the user. The ManagesType attribute is used via reflection to build a dictionary of EVERY View in our app that inherits from CustomView to "register" what object type it was designed to handle. The LibSpecSelectionView "broke that pattern" by displaying two different collections at the same time (creates two tabs and shows one custom collection in one tab and the other in the second tab) So the same view is capable of handling two different custom collections.
The dictionary of which views are capable of handling which collection types is then leveraged through an extension method to allow any of our custom collections to launch the registered view (or a default one if there is not a "registered" view) through a one line call to the view manager.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文