Using the. NET Framework to write Windows programs has become very simple in many aspects. In many cases, the. NET Framework is used, and the. NET compiler is bound to the Assembly metadata during compilation, making flexible Program Design easier. In fact, for. NET, it is possible to use embedded metadata to free us from the DLL hell.
Fortunately, the designers of the. NET Framework did not choose to elegantly hide the metadata. The designers gave us the reflection API. through reflection, A. NET program can view this metadata through programming. A program can reflect anything contained in a specific assembly, or all types and members contained in it.
Binding metadata to an executable Assembly provides many advantages. This allows the. NET assembly to be completely self-described. It also allows developers to share components across languages, removing the need for header files. (These header files will expire due to the relevant implementation code .)
All positive messages about. NET metadata seem hard to believe. It seems nothing, just a lie. However, it does exist. In. NET, you can create the metadata of your specific program and apply the metadata to what you can imagine.
By using custom features, developers can define the metadata of their own specific programs. Because the value of these features will become another part of the metadata, bound to a set of programs. Therefore, the values of these custom features can be checked and used by the reflection API.
We often mention the attributes of a class. The values of these attributes can be used as features. So what is the real difference between attributes and custom features?
Through this article, you will learn how to customize features, how to apply features to your source code classes and methods, and how to use the reflection API to obtain and use the values of these features.
How do I use features during Common Language Runtime?
Before you begin to consider how to use your own defined feature classes, let's look at some standard features that are already used when running in public languages.
The [WebMethod] feature provides a simple example. It can convert any public method in the subclass derived from WebService into a part of the Web Service exposure method, you can simply add [WebMethod] to the method definition.
Public class SomeWebService: System. Web. Services. WebService
{
[WebMethod]
Public DataSet GetDailySales ()
{
// Request processing code
}
}
You just need to add the [WebMethod] feature to a method,. NET will handle all the other things for you in the background.
When the [Conditional] attribute is used for a given method, whether the method is callable depends on whether the specified preprocessing identifier is defined. For example, see the following code:
Public class SomeClass
{
[Conditional ("DEBUG")]
&