Attributes in C#

2019-01-07 11:15发布

I know that C# (and .NET in general) is big on attributes. However, despite the fact I have programmed in C# for many years, I haven't found myself ever using them. Would someone get me started on them, and explain where is the best to use them?

Thanks

8条回答
叛逆
2楼-- · 2019-01-07 11:40

I use attributes for the following:

  • Communicating with a plug-in architecture
  • Telling another framework what to do with the code (NUnit, for instance)
  • Adding metadata for use with other code (See PropertyGrid)
  • Mapping objects to databases (See Castle ActiveRecord)
  • When writing my own APIs to allow users to communicate metadata
  • In framework code to tell the debugger to skip over it

Those are off the top of my head. I use them in many other places

查看更多
唯我独甜
3楼-- · 2019-01-07 11:41

I like to use attributes as metadata to my code. We have created some simple attributes that let us tag who wrote what code, when, and why. This lets us have both documented changes in code and in runtime. If there are any exceptions during runtime, we can inspect the callstack, look at any attributes on the methods along the way, and track down the people responsible:

[Author("Erich", "2009/04/06", Comment = "blah blah blah")]
public void MyFunction()
{
...
}

Of course, we could use our source control to look at who checked in what code, but this I've found makes the information more available in the place where you need it. Also, if we ever change source control, that information will not be lost since it is persisted in code.

查看更多
爷、活的狠高调
4楼-- · 2019-01-07 11:44

Attributes are very good at describing some runtime behaviour of your code that is orthoganal to the code in question. For example, in a class called Customer you would model a customer, right? But you might not want to model or describe the way a Customer object is serialized.

Adding attributes to your Customer class allows you to tell some other part of the runtime how it should deal with your Customer.

MSTest and NUnit makes use of attributes to tell the test framework how it should use classes that define test fixtures.

ASP.NET MVC uses attribute to tell the mvc framework which methods on classes it should treat as controller actions.

So, any place where you have a runtime behaviour that you wish to model attributes can be useful.

查看更多
不美不萌又怎样
5楼-- · 2019-01-07 11:46

Attributes are a form of declarative programming, 'similar' to creating your UI in XAML. It 'marks' pieces of code (classes, methods, properties, whatever) with an attribute so that you can later gather all those pieces marked in a specific way and then do something standard with all of them.

Eg. Consider the scenario where you have certain sections of code that you want to run once each time your app starts. In one model of programming (non-attribute) you go to your main method and explicitly call those init methods. With attributes you simply gather all methods which you've marked with your 'init' attribute, and call them via reflection.

The same pattern holds for actions like serialization, persistence and whatnot...

查看更多
霸刀☆藐视天下
6楼-- · 2019-01-07 11:48

Attributes get more use in code targeted to other programmers or between distinct parts of a program, rather than code targeted at end users.

For example, you could use attributes to import a dll, indicate how types would interact with visual studio (designer visible, intellisense helps, debugger step-through, etc), how to serialize them, indicate a type is obsolete, describe default values, descriptions, handle COM access, etc.

Those are things that are largely invisible to the end user and that a single programmer could put elsewhere in the source code. But they're useful when only the compiled binary is available and not the source.

查看更多
成全新的幸福
7楼-- · 2019-01-07 11:50

I believe you mean that you do not use (or frequently use) custom defined attributes ?

In my current project, I make heavy use of custom attributes, but, the fact that you need to keep in the back of your mind, is that using attributes should not be a goal on itself.

It is a tool / purpose to get to a given solution.

I sometimes use custom attributes in combination with a weaver like PostSharp, to decorate methods where some weaving should be applied to at compile-time.

In my current project, I also use attributes to decorate certain types with additional info ... But I believe I've posted about this here before: Cool uses of Attributes or Annotations (CLR or Java)?

查看更多
登录 后发表回答