我什么时候应该在 C# 中使用属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2295119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:10:40  来源:igfitidea点击:

When should I use attribute in C#?

c#attributes

提问by ccppjava

I saw some of the examples of utilize attribute, e.g. (as a map for dynamic factory) http://msdn.microsoft.com/en-us/magazine/cc164170.aspx

我看到了一些利用属性的例子,例如(作为动态工厂的地图) http://msdn.microsoft.com/en-us/magazine/cc164170.aspx

Just wondering what is the advantage of using attribute? I can find the reference on http://msdn.microsoft.com/en-gb/z0w1kczw(VS.80).aspxhowever, I am not sure when and why should I try to use it.

只是想知道使用属性有什么好处?我可以在http://msdn.microsoft.com/en-gb/z0w1kczw(VS.80).aspx上找到参考, 但是,我不确定何时以及为什么应该尝试使用它。

采纳答案by Asad

In the .NET Framework, attributes can be used for many reasons -- like

在 .NET Framework 中,可以出于多种原因使用属性——比如

  • Defining which classes are serializable

  • Choosing which methods are exposed in a Web service

  • 定义哪些类是可序列化的

  • 选择在 Web 服务中公开哪些方法

Attributesallow us to add descriptionsto classes, properties, and methods at design time that can then be examined at runtime via reflection.

Attributes允许我们descriptions在设计时添加类、属性和方法,然后可以在运行时通过反射进行检查。

Consider this example:

考虑这个例子:

Say you have a class which has a method from older version which is still in use for any reason and now you have come up with a new version of the class which makes fantastic use of Generic List and LINQ and has a new method for similar purpose. You would like developers to prefer the new one provided in the later version of your library. How will you do that ? One way is to write in the documentation. A better way is to use attribute as follow.

假设你有一个类,它有一个旧版本的方法,由于任何原因仍在使用,现在你想出了一个新版本的类,它很好地使用了泛型列表和 LINQ,并有一个用于类似目的的新方法. 您希望开发人员更喜欢您的库的更高版本中提供的新版本。你会怎么做?一种方法是写在文档中。更好的方法是使用属性如下。

public class AccountsManager
{
  [Obsolete("prefer GetAccountsList", true)]
  static Account[] GetAccounts( ) { }    
  static List<Account> GetAccountsList( ) { }      
}

If an obsoletemethod is used when the program is compiled, the developer gets this info and decides accordingly.

如果obsolete在编译程序时使用了某个方法,则开发人员会获取此信息并做出相应的决定。

AccountManager.GetAccounts() is obsolete: prefer GetAccountsList

AccountManager.GetAccounts() 已过时:更喜欢 GetAccountsList

We may also create and add Custom Attributesas per requirements.

我们也可以Custom Attributes根据要求创建和添加。

Reference :

参考 :



Hope this helps

希望这可以帮助

回答by RameshVel

The .NET Framework predefines and uses attribute types to control run-time behavior of the application.

.NET Framework 预定义并使用属性类型来控制应用程序的运行时行为。

Consider [webmethod] attribute, at runtime framework resolves this attribute and determine this method going to be exposed in a webservice.

考虑 [webmethod] 属性,在运行时框架解析此属性并确定此方法将在 web 服务中公开。

The same way, you can write your custom attributes to control the behaviour of your application at runtime. Attributes can target classes,methods,properties,delegate, enum, event, field...

同样,您可以编写自定义属性来控制应用程序在运行时的行为。属性可以针对类、方法、属性、委托、枚举、事件、字段...

To resolve the attribute at runtime, you must use reflection.

要在运行时解析属性,您必须使用反射。

Checkout the MSDNlink for more details.

查看MSDN链接以获取更多详细信息。

回答by Gerrie Schenck

Consider an attribute as metadata about the method or property it belongs to. It tells something more about a member.

将属性视为关于它所属的方法或属性的元数据。它讲述了更多关于成员的信息。

回答by Anders Fjeldstad

Attributes are appropriate when you want to attach metadata to your classes or class members, as well as when applying a common behaviour without having to implement a certain interface for each unit that shares the behaviour. The latter is an example of aspect-oriented programming.

当您想要将元数据附加到您的类或类成员时,以及在应用公共行为而不必为每个共享行为的单元实现特定接口时,属性是合适的。后者是面向方面编程的一个例子。

回答by Eric Lippert

My recommendation: use attributes to state facts about mechanisms, but not to model aspects of your business domain.

我的建议:使用属性来说明机制的事实,但不要对业务领域的各个方面进行建模

More details:

更多细节:

http://blogs.msdn.com/ericlippert/archive/2009/02/02/properties-vs-attributes.aspx

http://blogs.msdn.com/ericlippert/archive/2009/02/02/properties-vs-attributes.aspx