Define and use your own attributes in Visual C #

Source: Internet
Author: User

Complex, component-oriented business development, and expect modern software development engineers to have more flexible designs than past design methods. Microsoft's. The NET Framework uses the well-known declarative programming to attach additional functionality to a wide range of features. In software systems, features can enhance the elasticity of the system, because the feature makes the loose coupling of functions enhanced. So you can customize your own attribute classes and use these features loosely coupled to your own purposes.

Use. NET Framework to write Windows programs has become quite simple in many ways. In many cases,. NET Framework use,. NET compilers are bound to the assembly's metadata at compile time. Makes flexible programming easier. In fact, for. NET, it is possible to use embedded metadata to free us from DLL hell.

Thankfully, it's. NET Framework designers did not choose to hide the metadata gracefully. The designers gave us the reflection API, through reflection, a. NET program to view this metadata programmatically. A program can reflect whatever is contained within a particular assembly, or all types and members that are contained therein.

Binding metadata to an executable assembly provides a number of advantages. This makes. NET assemblies, which are completely self-describing. It also allows developers to share components across languages, removing the need for header files. (These header files expire because of the relevant implementation code.) )

About. NET metadata all positive news seems hard to believe, it seems like nothing, just a lie. However, it does exist. In. NET, you can create metadata for your own particular program, and you can apply that metadata to places you can imagine.

Developers can define metadata for their own specific programs by using custom attributes. Because the value of these attributes will become another part of the metadata, bound to an assembly. Therefore, the values of these custom attributes can be checked by the reflection API and can be used.

We often refer to the properties of a class, which can be used as attributes. So what's the real difference between attributes and custom attributes?

Through this article, you will learn how to customize features, how to apply attributes to your source code classes and methods, and how to use the reflection APIs to get and use the values of these attributes.

How does the common language runtime use attributes?

Before you start thinking about how to use your own defined attribute class, let's look at some of the standard features that are already useful in the common language runtime.

The [WebMethod] feature provides a simple example. It enables any public method in webservice derived subclasses to be translated into part of the Web service exposure method, which can be done simply by attaching [WebMethod] to the definition of the method.

public class SomeWebService : System.Web.Services.WebService
{
[WebMethod]
public DataSet GetDailySales()
{
//处理请求的代码
}
}

You just add the [WebMethod] attribute to a method. NET will be in the background for you to deal with all the other things.

If the [Conditional] attribute is used on a given method, then whether the method is callable will depend on whether the specified preprocessing identifier is defined. For example, look at the following code:

public class SomeClass
{
[Conditional("DEBUG")]
public void UnitTest()
{
//单元测试代码
}
}

This code shows whether the method of the class UnitTest () is valid, depending on whether the preprocessing identifier "DEBUG" is defined (the debug constant is already defined when compiling the debug version). What we might be more interested in is what really happened after using [Conditional]. When the condition fails, the compiler will stop all calls to the method, compared to the preprocessing instructions with the same functionality #if ... #endif, this method is more concise, and, using this feature, we do not need to do anything more.

The attributes use positional and named parameters. In an example where the [Conditional] attribute is used, the specific symbol is the positional parameter. Positional parameters are mandatory and you must provide.

Let's go back to the example of using the [WebMethod] feature and take a look at the named arguments. This feature has a description named parameter that you can use as follows:

[WebMethod(Description = "Sales volume")]

Named parameters are optional, and the value of the parameter is followed by the name of the parameter. If there are positional arguments, then you need to write the positional arguments and then write the named arguments after the positional arguments.

I'll tell you more about positional parameters and named arguments later in the article, as I'll show you how to create and use your own attribute class.

Attributes can be used at run time, at design time

In this article, I provide all the examples that are relevant to the behavior of the runtime. However, binary files (assemblies) are not only used for runtime. In. NET, the metadata you define is not just limited to runtime, but instead, when you compile the assembly, you can refer to the metadata at any time.

Consider some possible scenarios in which metadata is used at design time. In Visual Studio.NET, you can use the IDE to build tools (using. NET language) to facilitate development and design (wizards, builders, and so on). In this way, the runtime environment of a module (e.g. IDE tools) becomes the design-time environment of another module (the source code developed). A good example of using custom features is provided here. The IDE tool will reflect the classes and types you write, and then follow your code. Unfortunately, because there is no code for IDE tools, exploring such examples is beyond the scope of the article.

Standard. NET feature contains a similar example. When a developer creates a custom control and puts it in the Visual Studio.NET IDE's toolbox, they (custom controls) already use a series of attributes that explain how to handle custom controls in a property sheet. Table1 enumerates and describes the 4 standard. NET features that are used in the property sheet.

Table 1: The standard. NET attributes that are in the design-time property sheet in the visual Studio. NET IDE.

Attribute

Description
Designer Specifies the class that is used to implement design-time services for the component.
Defaultproperty Specifies the default property of the component in the property sheet.
Category Specifies the category of the property in the property sheet.
Description Specifies a description of the property in the property sheet.

These form-related features let us realize that you can use attributes and their values at design time, just as you would at runtime.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.