当前位置:网站首页>C Advanced (feature)

C Advanced (feature)

2022-06-10 06:38:00 Don't ask, ask is a program ape

characteristic

         What are features

describe C# Information about some types of data in

         The role of characteristics

Used to apply metadata or declaration information to code ( Assembly 、 type 、 Method 、 Properties, etc ) Related to

         Characterization

Features can be like adding metadata to a program ( Variable )
You can apply one or more features to the entire assembly 、 Modules or smaller program elements ( Such as classes and properties ) A program element can add multiple features
Properties can accept parameters in the same way as methods and properties Features can accept parameters
Programs can use reflection to check their own metadata or metadata within other programs
characteristic ( Metadata ) It is defined after compilation

        Metadata

Metadata (Meta Data) Metadata is a kind of binary information , For storing portable executable files in common language runtime (PE) File or program stored in memory . Compile your code into PE When you file , The metadata will be inserted into a part of the file , And convert the code to Microsoft Interlingua (MSIL) And insert it into another part of the file . Each type and member defined and referenced in a module or assembly is described in metadata . When executing code , The runtime loads metadata into memory , And reference it to find classes about the code 、 member 、 Inheritance and other information .

        Characteristic classification

                 Predefined feature (.NET)

                        AttributeUsage

AttributeUsage

Predefined feature AttributeUsage It is mainly used to identify the types of program elements to which custom features can be applied , This information is given by the first parameter .

[AttributeUsage(

        validon,// Specify the language elements where features can be placed , It's an enumerator AttributeTargets A combination of the values of , The default is AttributeTargets.All.

        AllowMultiple=allowmultiple,// If true, This feature can be used multiple times in the same element , The default is false( Can not be used more than once ).

        Inherited=inherited,// If true, The property can be inherited by a derived class , The default value is false( Don't be inherited )

)]

[AttributeUsage(              AttributeTargets.Class |                ( class )

                                        AttributeTargets.Constructor |     ( Structure )                                                                                            AttributeTargets.Field |                 ( Field )

                                        AttributeTargets.Method |             ( Method )

                                        AttributeTargets.Properties,      ( attribute )

                                        AllowMultiple = true                      ( all )

)]                   

                        Conditional

Conditional This predefined feature marks a conditional method , Its execution depends on its top preprocessing identifier . It causes conditional compilation of method calls , Depends on the specified value , such as Debug and Trace

#define DEBUG        // Definition DEBUG macro , Then there is output below , No definition, no output
using System;
using System.Diagnostics;
public class Myclass
{
    [Conditional("DEBUG")]
    public static void Message(string msg)
    {
         Console.WriteLine(msg);   
    }        
}

                        Obsolete

Obsolete This predefined feature marks program entities that should not be used . It lets you tell the editor to discard a specific target element . for example , When a new method is used in a class , But you still want to keep the old methods in the class , You can display a new method that you should use by prompting , Not the old method message , Let's label it as obsolete( Out of date ).

[Obsolete(string " What will be prompted when using this method ",bool error( Prohibited or not )]

static void OldMethod()

{

        Console.WriteLine("It is the old method");

}

public static void Main()

{

        OldMethod();

}

                Custom features


Custom features

Custom attributes should be derived from System,Attribute class

// A custom feature BugFix Assigned to a class and its members

[AttributeUsage(      AttributeTargets.Class |                ( class )

                                  AttributeTargets.Constructor |     ( Structure )                                                                                  AttributeTargets.Field |                 ( Field )

                                  AttributeTargets.Method |             ( Method )

                                  AttributeTargets.Properties,      ( attribute )

                                  AllowMultiple = true                      ( all )

)]                  

public class Demo : System.Attribute

{

        public Demo(int bg,string dev,string d)

        {

                this.bugNo = bg;

                this.developer = dev;

                this.lastReview = d;

        }

}

[Demo(45,"Weirdoand","12/8/2012",Message = "Return type mismatch")]

Class DemoClass{}

Unity Provide us with custom features

Header

System.Serializable

Range

...

原网站

版权声明
本文为[Don't ask, ask is a program ape]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203021217548361.html