Server Control-knowledge-application properties-Notes (9)

Source: Internet
Author: User

Http://msdn.microsoft.com/zh-cn/library/bfz783fz (vs.80). aspx

Apply the attribute to the code element through the following process:.

  1. You can import a namespace from the. NET Framework to define a new property or use an existing property.

  2. You can apply an attribute to a code element by placing it next to the code element.

    Each language has its own attribute syntax. In C ++ and C #, attributes are enclosed by square brackets and are separated by elements by spaces (including break characters. In Visual Basic, attributes are enclosed by Angle brackets and must be in the same logical line. If you need to add a line character, you can use the row to continue the character. In J #, use the special annotation syntax to add attributes.

  3. Specify the location and name parameters for the attribute.

    Location parameters are required and must be placed before all naming parameters. location parameters correspond to parameters in one of the property constructors. The name parameter is optional and corresponds to the read/write property of the attribute ). In C ++, C #, and J #, specify name = value for each optional parameter, where name is the attribute name. In Visual Basic, specify name: = value.

Attributes are sent to metadata when you compile the code, and can be used by the runtime reflection Service for Public Language Runtime libraries and any custom tools or applications.

According to the conventions, all attribute names end with attribute. However, in some languages (such as Visual Basic and C #) that target the Runtime Library, the full name of the attribute is not required. For example, to initialize system. obsoleteattribute, you only need to reference itObsoleteYou can.

Apply attributes to methods

The following code example shows how to declareSystem. obsoleteattributeThis property marks the code as obsolete. String"Will be removed in next version"Passed to this property. When the code described by this property is called, this property generates a compiler warning to display the passed string.

C# using System;public class Example{    // Specify attributes between square brackets in C#.    // This attribute is applied only to the Add method.    [Obsolete("Will be removed in next version.")]    public static int Add(int a, int b)    {        return (a + b);    }}class Test{    static void Main()    {        // This generates a compile-time warning.        int i = Example.Add(2, 2);    }}
Obsolate attributesUsing system; public class anyclass {[obsolete ("Don't use old method, use new method", true)] Static void old () {} static void new () {} public static void main () {old () ;}} in this example. we use the obsolete attribute to mark a program entity that should no longer be used.
The first parameter is a string used to explain why the item is voided and used instead.
In fact, you can write anything here.
The second parameter tells the compiler that the place where this item is used should be treated as an error.
The default value of this parameter is false, which means that the compiler generates a warning. when we try to compile the above program, we will get an error: anyclass. old () 'is obsolete: 'don' t use old method, use new Method'
Application properties at the Assembly level

To apply properties at the Assembly level, useAssemblyKeyword. The following code displaysAssemblynameattribute.

C #

Using system. reflection;
[Assembly: assemblyname ("myassembly")] when this attribute is applied, the string"Myassembly"Put in the Assembly List of the file metadata section. You can use the msil disassembly program (ildasm.exe) or create a custom program to retrieve this property to view this property.

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.