Hello.
In this video, we're going to talk about Question 17.
What are attributes, attributes of metadata to a type?
In other words, there are a way to add information about a type or method to the existing metadata,
which describes that type or method, which we can read from the type object.
First, let's understand what metadata is, generally speaking.
Metadata is data providing information about other data.
For example, when working with databases, the data stored inside the database is the actual data.
While the structure of tables and relations between them is metadata in programming, metadata describes
types used in the application.
First, let's consider this simple person class.
There is a lot of metadata describing this class.
For example, the metadata contains the information that this class is named person.
It is public, not static, non-zero, etc. It contains too good only public properties called name
and year of birth.
It has one public constructor taking two three parameters and one taking one parameter.
The actual data stored in an instance of this class would be the string representing the name and int
representing the year of birth.
We can access all the classes metadata using reflection.
What we learned about in the previous lecture.
Sometimes we want to add extra metadata to a type or a method, and this is what attributes our form.
Let's consider the following example You want to have a common way of validating some data in the application.
No matter the type, we want to be able to specify that some members, for example, 3GHz, must have
certain length.
This is what they want.
I want to create some validating grass that will be able to take objects off any grass and took it for
any of their properties.
This validation is required.
If so, it should check if the values of those properties are valued.
First, let's write how we would like to use the validator and then we'll implement it.
All right.
So what we want to do is to add some more metadata, the name properties in both person and dog types,
we found there are minimal and maximal lengths.
This is some extra metadata and to define it, we must use a custom attribute.
This is how I would like to use it.
To add an attribute to a member or type, we simply must write its name in the brackets above the type
or member who wants to add it to.
As you can see, this attribute will require two parameters.
Minimal and maximal length.
Now let's define the string length validate attribute graphs.
All attributes must derive from the attribute the base class also its name should earn with attribute.
As you see here, this postfix is omitted when we actually use the attribute.
Well, so that's it.
These attribute must only hold the metadata about the minimal and maximal length of this -- property.
One more thing, though, we can also define what the attribute can be applied to.
In our case, we want it to be applied to properties.
To enforce that, we must actually use a built in attribute called attribute usage.
All right.
We added this metadata to the person and dog process.
Now you must implement the validator across.
This grass simply contains validated method, which can take an object for this object will look for
its properties.
Will the three grant validate attributes defined?
Here we select those properties of this type for which this attribute is defined using the links were
matured along with attribute is defined method.
Now we can iterate those properties and for values, check their legs are correct.
But first, we must make sure that the property is a string.
If not, we want to throw an exception because it means that a developer others this attribute to a
different type by mistake.
Now, when we are sure that the value of the property is string, we can validate it.
All right.
Let's see what happens here.
First of all, I've cast the property value to string.
I know it's safe because I already checked it here.
Then I needed to access the attribute object to do so, I called the Get Custom Attributes metric on
the property.
It takes the type of the attribute and a Boolean saying if you should also check the attributes that
have been inherited from the base type.
This returns a correction and they took the first element from it.
Here happens the actual validation and based on it, I returned either false or true.
All right, let's make sure it works.
The result seems correct.
The person is valid, but the dog isn't because the property name has only one letter.
As you can see, attributes can be quite powerful.
They are widely used in native grasses as well as external libraries.
For example, if you ever used and you need, you must have used some of its attributes.
Let's summarize attributes at metadata to a type.
In other words, they are a way to have information about a type or some of its members to the metadata
which describes it to add attributes to our member or type.
We must simply write its name in the brackets above the thing we want to add it to.
There are plenty of built in attributes in such a standard library, but we can also create attributes
of our own.
Simply by creating classes derived from the attribute based grass during the interview, you might be
asked what this method they call metadata is data describing of our data.
Like with databases, the metadata is the structure of tables and relations between them in programming.
Metadata describes types used in the application.
Who can access it by using reflection and the this way?
Good information about some type, for example, what methods or what constructors it contains.
How to define a custom attribute simply by defining a class derived from that attribute based class.
All right, that's it for this lecture.
Thanks for watching, and I'll see you in the next one.