What is C#metadata?

Asked

Viewed 628 times

17

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

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

1 answer

13


Within the Common Language Infrastructure used by . NET it is foreseen the use of these metadata to give more information about the operation of the applications. This information can be the most varied and can be entered in various ways, through compilers and other tools (from AOP, for example).

Virtually any part of the generated code to run inside the CLR has some metadata stating what that is. Often this is done indirectly through attributes (some examples).

This information is essentially descriptive and ranges from versions, signatures and information that are not relevant to the code itself, to information that is crucial to the functioning of the CLR and the . 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, Visual Studio, other tools, CLR itself in several of its components, including Jitter and GC and . NET and their sub-frameworks, in addition to the users' own applications.

This information can be accessed through reflection. You can also use an external tool to query this outside of execution, such as ildasm.exe (already has better).

Think of this data as properties of code elements, but at the bottom the name is even attribute. 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 not directly part of it.

I believe the questions linked shows well how to use it. In official documentation has examples of how to use reflection to read this data. It has how write new custom attributes. And has as apply attributes to code members.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.