Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Attributes and attribute location/targets

Tags:

c#

attributes

I'm reading up about attributes and understand that they can be made to apply to different target entities with you code - (see Attribute Targets).

So, looking at the AssemblyInfo.cs file in my project, I can see the following:

[assembly: AssemblyTitle("AttributesDemo")]
[assembly: AssemblyDescription("")]

Which makes sense to me. An attribute whose target is the assembly.

In my code I can add an attribute on a class as follows:

[MyAttribute]
class MySerialzableClass
{

With MyAttribute being:

[AttributeUsage (AttributeTargets.All)]
public class MyAttribute : System.Attribute
{
}

So, I got to thinking about the assembly: statement in the first code block. And tried this, just for experimentation:

[class: MyAttribute]
class MySerialzableClass
{

This gives the compiler warning:

'class' is not a recognized attribute location. All attributes in this block will be ignored.

So my question is this - Why do have to specify the Attribute Target on some attributes and are not requried, or permitted to for others? Moreover, for which ones must you do this?

like image 823
James Wiseman Avatar asked Mar 04 '10 15:03

James Wiseman


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


3 Answers

you have to specify target explicitly in case where the target is not represented in code. I know only three that targets, assembly, module and return:

[return: MyAttribute]
public static int meth(

for class specifying class: is excessive, compiler can understand what do you meant

like image 162
Andrey Avatar answered Oct 20 '22 00:10

Andrey


You can specify attribute targets for any attribute usage, but only the ones that don't have a default (assembly and module) are mandatory. Also, you must use these annotations when you want to apply an attribute to a non-default target.

Examples of non-default targets:

[return: MyAttribute]   
public int Method() { ... }

public int Property {
  get;
  [param: MyAttribute] // applies to the parameter to the setter
  set;
}

In your example, the right target (which is the default) is type:

[type: MyAttribute] 
class MySerialzableClass { }
like image 43
Jordão Avatar answered Oct 19 '22 22:10

Jordão


Normally, the attribute comes right before what it affects, such as the class or method. For assembly-wide attributes, there's no "before", so you have to specify.

like image 44
Steven Sudit Avatar answered Oct 19 '22 22:10

Steven Sudit