Reflection and attributes

Source: Internet
Author: User

• Concepts of reflection

• Reflection traversal members

• Member of the call type

• Attribute)

• Attribute instance

• Custom Attributes

• Three attributes

Reedit once run

After one compilation

Reflection

Reflection is the behavior of programmatically reading metadata associated with the type. By reading metadata, you can understand what type it is and what type it is a member. Such as attributes, methods, and events in the class.

Namespace system. Reflection

Reflection-reflection member name

Class

Class demo_class

{

Public demo_class (int I)

{

Console. writeline ("constructor:" + I );

}

Public void method (string S)

{

Console. writeline ("Class A parameter:" + S );

}

Public int I;

Public String s

{

Get; set;

}

}

Call

Static void main (string [] ARGs)

{

Type type = typeof (demo_class );

Memberinfo [] MI = type. getmembers (bindingflags. nonpublic | bindingflags. Public | bindingflags. instance );

Foreach (memberinfo MI in MI)

{

Console. writeline ("Name: {0}, type: {1}", mi. Name, mi. membertype. tostring ());

}

}

Running result

Reflection-use reflection to call a member without parameters to construct a type

Class demo_class

{

Public void method (string S)

{

Console. writeline ("Class A parameter:" + S );

}

}

Class Program

{

Static void main (string [] ARGs)

{

Bindingflags BF = bindingflags. declaredonly | bindingflags. Public | bindingflags. nonpublic | bindingflags. instance;

Type T = typeof (demo_class );

Constructorinfo cons = T. getconstructor (New Type [0]); // The constructor has no parameters, so the constructor has no type parameters.

Object OBJ = cons. Invoke (null); // The input constructor parameter is null.

Object [] methodpar = new object [] {"A"}; // method parameters

Methodinfo MI = T. getmethod ("method", BF); // obtain the Method

Console. writeline (MI. Invoke (OBJ, methodpar ));

}

}

Reflection-use reflection to call a member with parameters to construct a type

Class demo_class

{

Public void method (string S)

{

Console. writeline ("Class A parameter:" + S );

}

}

Class Program

{

Static void main (string [] ARGs)

{

Bindingflags BF = bindingflags. declaredonly | bindingflags. Public | bindingflags. nonpublic | bindingflags. instance;

Type T = typeof (demo_class );

Constructorinfo cons = T. getconstructor (New Type [] {typeof (INT)}); // The constructor has parameters and the type is int.

Object OBJ = cons. Invoke (net object [] {123}); // input the construction parameter to obtain the object

Object [] methodpar = new object [] {"A"}; // method parameters

Methodinfo MI = T. getmethod ("method", BF); // obtain the Method

Console. writeline (MI. Invoke (OBJ, methodpar ));

}

}

Attribute-attribute

Attribute non-property (class member)

Attribute provides powerful methods to associate declarative information with C # code (types, methods, attributes, and so on.

After attributes are associated with program entities, you can use the technology "reflection" to query attributes at runtime.

Attributes appear in two forms:

1. attributes defined in the Common Language Runtime Library (CLR.

2. Another custom attribute that can be created to add additional information to the code. This information can be retrieved programmatically later.

Attributes have the following features:

1. attributes can be added to the program. Metadata is information in an embedded program, such as compiler instructions or data descriptions.

2. The program can use reflection to check its metadata.

3. Attribute interaction with COM is usually used.

Example:

[System. runtime. interopservices. dllimportattribute ("user32.dll", entrypoint = "messageboxw")]

Public static extern int messageboxw ([system. runtime. interopservices. inattribute ()] system. intptr hwnd, [system. runtime. interopservices. inattribute ()] [system. runtime. interopservices. marshalasattribute (system. runtime. interopservices. unmanagedtype. lpwstr)] string lptext, [system. runtime. interopservices. inattribute ()] [system. runtime. interopservices. marshalasattribute (system. runtime. interopservices. unmanagedtype. lpwstr)] string lpcaption, uint utype );

Static void main (string [] ARGs)

{

Console. writeline (messageboxw (intptr. Zero, "OK? "," Prompt ", 1 ));

}

Custom Attributes

You can create your own custom attributes by defining an attribute class. This attribute class is derived directly or indirectly from system. attribute, which helps you easily and quickly identify attribute definitions in metadata. Suppose you want to mark the class and structure by the name of the programmer who writes the class or structure.

[System. attributeusage (system. attributetargets. class | system. attributetargets. struct, allowmultiple = true, inherited = true)] // The author attribute can only be used for classes and structures. Whether allowmultiple allows multiple attributes, inherited indicates that this attribute is stuck to the subclass.

Public class Author: system. Attribute

{

Private string name;

Public double version;

Public author (string name)

{

This. Name = Name; version = 1.0;

}

}

[Author ("Zhang San", version = 2.0)] // Zhang San is the constructor parameter of author, and version is the field

Class sampleclass

{}

Three special attributes

1. attributeusage attribute (as shown in the above example)

2. Conditional attributes

3. Obsolete attributes

Three special properties-conditional

The condition method must be a method in the class or structure declaration, and must have a void return type.

# Define trace_on // This line of Identification Code determines whether the red code is executed or not.

Using system;

Using system. diagnostics;

Namespace r_a_demo

{

Public class trace

{

[Conditional ("trace_on")]

Public static void MSG (string MSG)

{

Console. writeline (MSG );

}

}

Public class programclass

{

Static void main ()

{

Trace. MSG ("debugging information ");

Console. writeline ("code body ");

}

}

}

Another usage

# Define trace_on

Using system;

Using system. diagnostics;

Namespace r_a_demo

{

Public class trace

{

# If trace_on

Public static void MSG (string MSG)

{

Console. writeline (MSG );

}

# Endif

}

Public class programclass

{

Static void main ()

{

Trace. MSG ("now in Main ...");

Console. writeline ("done .");

}

}

}

Three special properties-obsolete

[System. Obsolete ("use Class B")] // the class will be warned during instantiation

Class

{

Public void method (){}

}

Class B

{

[System. Obsolete ("use newmethod", false)] // message displayed during method calling

Public void oldmethod (){}

[System. Obsolete ("use newmethod", true)] // cannot be compiled

Public void newmethod (){}

}

Summary:

Reflection: Decompress the compiled result to obtain the type and type members.

Attribute: type of additional information for other types.

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.