What are metadata in C #?

16

What are metadata in C # / .NET? What are they good for, and how can I use them?

I was looking at a template of Visual Studio, and browsing through the files, I came across them.

    
asked by anonymous 07.01.2016 / 05:04

1 answer

13

Within the Common Language Infrastructure used by .Net, it is intended to use this metadata to provide more information about how applications work. This information can be the most varied and can be inserted in many ways, through compilers and other tools (for example AOP).

Nearly any part of the code generated to run within the CLR has some metadata telling you what that is. This is often done indirectly through the attributes ( some examples ).

This information is essentially descriptive, ranging from versions, signatures, and information that are few and far between relevant to the code itself, to crucial information for the operation of CLR and .NET as a whole. In general they are markings about how a type is defined, how a method should be used, auxiliary information for members of a type, constraints for parameters, just to name a few examples. They can be used to configure certain aspects by language compilers, by Visual Studio, other tools, the CLR itself in several of its components, including JITter and GC and .Net and its sub-frameworks , in addition to the users' own applications.

This information can be accessed through reflection . You can also use an external tool to check this out of execution, such as ildasm.exe .

Think of these data as properties of the code elements. They are adjectives of what each part of the code should be or behave. It's like creating a database with additional information that is related to the code but is not part of it directly.

I believe that the linked questions shows how much use should be made. At official documentation you have examples of how to use reflection to read this data. You can write new custom attributes . And you have apply attributes to code members .

An example use would be to create a label attribute to use in class properties that serve as a template for some presentation framework . Then it is possible to generate a "screen" automatically with the members of this class by printing the name next to the fields according to these attributes.

    
07.01.2016 / 05:39