Attribute (i)--pre-defined features

Source: Internet
Author: User

in the project, I was exposed to Attribute , then what is Attribute, What role does it have? Here to find out.

first, what is Attribute

Attribute Class Associates pre-defined system information or user-defined custom information with a target element. The target element can be an assembly, class, constructor, delegate, enumeration, event, field, interface, method, portable Executable module, parameter, property , return value, structure, or other property ( Attribute)

here, We need to put attribute and Span lang= "en-US" style= "FONT-FAMILY:CALIBRI; Background: #CCFFFF ">property separate

Property refers to the field that the class would like to provide, it belongs to the object-oriented level of the abstract class, a state description of the class, such as we according to the characteristics of the American high nose, yellow hair, blue eyes and other characteristics, to abstract a Americans This class, then the high nose, yellow hair, blue eyes is American of this class Property

and Attribute is a feature of a class or other thing that is described when the program is compiled or run.

For example, we declare two different methods in the program, but one is originally declared and obsolete, so we can use Attribute to identify, let it tell the program, this cannot be used. It belongs to the grammar of the programming language. Like a protocol or a rule, it's not necessarily the property that an object has.

Second, use

through the above introduction, should be to Attribute with a general idea, let's take a look at the specific application.

It can be divided into two categories: 1 , pre-defined: Microsoft has packaged the  

2 , custom: It needs to be defined and used by itself. Provides a good extension for the user

first of all, what are the predefined Attribute

1 , Conditional

when a method that is marked as conditional is called, the presence or absence of the specified preprocessing symbol determines whether this call is included or omitted. If the symbol is defined, the call is included, otherwise the call is omitted. Use Conditionalis closed#if and the #endif an alternative approach to internal methods, which is cleaner, more chic, and reduces the chance of error

Instance:

#define YANYAN//macro definition must be preceded by all code using System.diagnostics;class program    {        static void Main (string[] args)        {            a.b ();            A.C ();        }    }    public class A {        [Conditional (' Yanyan ')] public        static void B () {            Console.WriteLine ("Hello");        }        [Conditional ("Lixue")]        public static void C () {            Console.WriteLine ("World");        }    }

need here Note is that if you do not have a method that defines any conditions, the default is to always execute this method as long as it is called . If you want to judge execution by condition, then the method must contain at least one Conditional attribute, it responds to the condition you define . #define

Define multiple conditions:

Logical OR

[Conditional ("Yanyan"), Conditional ("Wyy")]

public static void B () {

Console.WriteLine ("Hello");

}

Logic with: MSDN provided a method, but failed to do so, and did not fully understand its meaning

2 , Obsolete

ObsoleteAttribute applies to all program elements in addition to components, modules, parameters, and return values. marks an element as stale informing the user that the element will be deleted in a future version of the product.

Instance:

using   System;using system.reflection;public class example{//Mark Oldproperty as Obsolete. [ObsoleteAttribute ("This property is obsolete. Use NewProperty instead. ", false)] Public static string Oldproperty {get {return  ' the old property V Alue. ";}} Public static string NewProperty {get {return  "the new property value.";}}   Mark Calloldmethod as Obsolete. [ObsoleteAttribute ("This method is obsolete. Call Callnewmethod instead. ", true)] public static string Calloldmethod () {return  Ed Calloldmethod. ";}   Public static string Callnewmethod () {return  "you have called Callnewmethod.";}      Public static void Main () {Console.WriteLine (oldproperty);      Console.WriteLine ();   Console.WriteLine (Calloldmethod ()); } }

Note : Here, if you give obsolete The second parameter is assigned a value, false represents compile time, if this method is called, a warning is raised, but it is not treated as an error, otherwise true cannot compile successfully for error

3 , AttributeUsage

determines how custom attribute classes can be used. attributeusage is an attribute that can be applied to custom attribute definitions to control how new features are applied.

This feature has three main parameters: 1 , AttributeTargets : A target, that is, a specific object that is valid for this attribute, may be a class or method, etc. can also be All

2 , AllowMultiple : Allow multiple entities to be used multiple times

3 , inherited : allows a class derived from an attributed to inherit it

Since this class is for a custom class, the specific application should be understood from the custom feature.

Summary: These are some of the basics of pre-defined features, including three classes, Conditional,obsolete and the AttributeUsage, One should note the meanings of their respective parameters when they are used, and the next blog summarizes some knowledge and applications about custom features


Attribute (i)--pre-defined features

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.