Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attributes of a class are stored in?

Tags:

c#

Are they stored in class methods or instanced methods? Or is it both? I am really looking for an explanation, and after much research I couldn't come to a definite answer.

Thanks

like image 253
user2133925 Avatar asked Oct 13 '13 11:10

user2133925


2 Answers

Going for precision here, all the [attributes] used in an assembly are gathered together by the compiler and written to the metadata of the assembly. Two tables in the metadata play are role.

The first one is the CustomAttribute table, it contains a list of every single attribute. The list entry has an index to the original declaration that had the attribute (1), a reference to the constructor for the attribute type (2) and an index for the value that's used to construct the attribute object at runtime (3).

The second one is the Blob table, the value index (3) in the CustomAttribute table indexes it. It contains the values you used in the attribute declaration. Those values are a string if you used a string or typeof argument, the actual value if you used a value type value argument, an array of those values if you use an array.

Constructing the attribute object at runtime thus involves first finding the entry in the CustomAttribute array by (1). Then locating the constructor for the attribute class by using (2) and jit compiling it if necessary. (3) is used to lookup the entry in the blob table, converting the values in the blob as necessary, like retrieving the Type from the string, creating the array, etcetera. And calling the constructor to create the object.

Organizing it that way has the big advantage that a declaration can have an arbitrary number of attributes and that attributes can be used on many kinds of declarations (assembly, type, method, parameter, field, etc). The disadvantage is that finding them back isn't particularly cheap.

like image 118
Hans Passant Avatar answered Oct 21 '22 00:10

Hans Passant


As commented by knittl in the comments(which is correct) you may check this MSDN:

The .NET Framework lets you declare specific kinds of metadata, called attributes, in your compiled file. Attributes can be found throughout the .NET Framework and are used to control in more detail how your program behaves at run time. Additionally, you can emit your own custom metadata into .NET Framework files through user-defined custom attributes. For more information, see Extending Metadata Using Attributes.

From the docs(CLI Partition II - Metadata and File Format ( word/pdf zip)):-

Attributes of types and their members attach descriptive information to their definition. The most common attributes are predefined and have a specific encoding in the metadata associated with them (§23). In addition, the metadata provides a way of attaching user-defined attributes to metadata, using several different encodings.

From MSDN:

You can use the members of the Type class to get the individual methods and members in the passed class. This example first queries the Type object to get attribute information for the class level. Next, it uses Type.GetMethods to place instances of all methods into an array of System.Reflection.MemberInfo objects to retrieve attribute information for the method level. You can also use the Type.GetProperties method to check for attributes on the property level or Type.GetConstructors to check for attributes on the constructor level.

like image 24
Rahul Tripathi Avatar answered Oct 21 '22 00:10

Rahul Tripathi