This article can be downloaded from a technical article, which contains a Visual Studio project sample file that uses custom attributes.
Attribute classes are special classes that can be applied to classes, properties, and methods at design time. An attribute class provides a way to describe certain aspects of an element or to determine the behavior of other classes attached to the element, which can then be accessed and validated at run time. You can see the attribute class as a way to add a special modifier as a class member.
For example, if you have ever written a Web service, you must know that to make the method public throughout the service, you have to use the WebMethod attribute. This is a good example of a demo attribute application, because we want to extend the programming model with the WebMethod attribute. There is no built-in way in C # to specify that a method is visible through a Web service (because there is a built-in way to indicate that a method is private), so you need to add the WebMethod property to meet this need.
Designing Custom Properties
The process of designing a custom attribute is simple, and you only need to consider the following aspects before you design properties:
What is the purpose of using attributes?
Properties can be used in many ways. You need to define exactly what the attributes are going to accomplish and make sure that the specific features are not built inside. NET Framework set. Use. NET modifier is better than using attributes because it simplifies integration with other components.
What information must the property store?
property is a simple flag that you intend to use to indicate a feature? Or does the property want to store information? A property can have a set of information given at design time and view that information at run time. For example, look at the alias properties in the example application.
Which fitting should the attribute reside in?
In most cases, you can include an attribute in an assembly that uses that property. However, there are examples where it is better to host attributes in a public, lightweight, shared-fitting assembly. This type of configuration allows customers to use attributes without reference to unwanted assembly parts.
Which accessories will recognize attributes?
If no module reads the attribute, it will be worthless. You are likely to place the class that reads the attribute in the same assembly as the property resides. However, as mentioned earlier, there are examples where you want to place the method of reading a property in a separate assembly from the property itself.