Reflection Learning Series 1-Introduction to reflection

Source: Internet
Author: User

 

Reflection Learning Series directory

Reflection Learning Series 1-Introduction to reflection

Reflection Learning Series 2-feature)

Reflection Learning Series 3-reflection instance applications

Author

 

 

Reflection, which is translated into Reflection in Chinese. This is.. Net ,. net applications are composed of several parts: 'assembly ', 'module', and 'Type (class) '. Reflection provides a programming method, this allows programmers to obtain relevant information about these components during the running period. For example, the Assembly class can obtain information about the running accessories and dynamically load the accessories, find the type information in the accessories and create an instance of this type. The Type class can obtain the Type information of an object. This information includes all elements of the object, such as methods, constructors, and attributes. The Type class can obtain and call the information of these elements. MethodInfo contains information about the method. You can use this class to obtain the name, parameter, and return value of the method and call it. For example, FieldInfo and EventInfo are included in the System. Reflection namespace.
I. Type is used to obtain Type information.
The System. Type class plays a core role in reflection. When a reflection request loads a Type, the common language runtime creates a Type for it. You can use the method, field, attribute, and nested class of the Type object to find all information about the Type.
You can clearly understand the Type by running the Pipeline Code and analyzing the result.

 

Obtain type information
Namespace ConsoleApplication2
{
Class Program
{
Static void Main (string [] args)
{
MyClass m = new MyClass ();
Type type = m. GetType ();
Console. WriteLine ("type Name:" + type. Name );
Console. WriteLine ("class Full name:" + type. FullName );
Console. WriteLine ("Namespace name:" + type. Namespace );
Console. WriteLine ("collection name:" + type. Assembly );
Console. WriteLine ("Module name:" + type. Module );
Console. WriteLine ("base class name:" + type. BaseType );
Console. WriteLine ("whether class:" + type. IsClass );
Console. WriteLine ("Public member of the class :");
MemberInfo [] memberInfos = type. GetMembers (); // get all public members
Foreach (var item in memberInfos)
{
Console. WriteLine ("{0 }:{ 1}", item. MemberType, item );
}
}

}
Class MyClass
{
Public string m;
Public void test ()
{}
Public int MyProperty {get; set ;}

}
}

 

Ii. Obtain Assembly metadata
The Assembly class defines an Assembly, which is a reusable, version-free, and self-describing block of the Common Language Runtime application. Because the Assembly uses metadata for self-description, we can get the internal structure of the Assembly through its metadata. Combined with Assembly and reflection, you can obtain the Assembly metadata, but you must first load the Assembly into the memory. You can use multiple static Load methods of the Assembly class to Load the Assembly.
The following program displays assembly information

 

Code
Public static void Main ()
{
// Obtain the assembly of the currently executed code
Assembly assem = Assembly. GetExecutingAssembly ();

Console. WriteLine ("assembly Full name:" + assem. FullName );
Console. WriteLine ("assembly Version:" + assem. GetName (). Version );
Console. WriteLine ("initial assembly location:" + assem. CodeBase );
Console. WriteLine ("assembly Location:" + assem. Location );
Console. WriteLine ("assembly entry:" + assem. EntryPoint );

Type [] types = assem. GetTypes ();
Console. WriteLine ("types contained in the Assembly :");
Foreach (var item in types)
{
Console. WriteLine ("class:" + item. Name );
}

}

 

Iii. Dynamic Loading type
Early binding is to bind the object type during compilation, while late binding is to bind the object type only during runtime. Reflection enables late binding, that is, dynamic loading types, and calls their methods. The following is an example in MSDN. For detailed explanations, see annotations.

 

Dynamic Loading type
Namespace ConsoleApplication2
{
Public class Example
{
Private int factor;
Public Example (int f)
{
Factor = f;
}

Public int SampleMethod (int x)
{
Console. WriteLine ("\ nExample. SampleMethod ({0}) executes.", x );
Return x * factor;
}

Public static void Main ()
{
// Obtain the assembly of the currently executed code
Assembly assem = Assembly. GetExecutingAssembly ();

Console. WriteLine ("Assembly Full Name :");
Console. WriteLine (assem. FullName );

// The AssemblyName type can be used to parse the full name.
AssemblyName assemName = assem. GetName ();
Console. WriteLine ("\ nName: {0}", assemName. Name );
Console. WriteLine ("Version: {0}. {1 }",
AssemName. Version. Major, assemName. Version. Minor );
Console. WriteLine ("\ nAssembly CodeBase :");
Console. WriteLine (assem. CodeBase );
// Create an Example instance from the assembly and point to it with the object-type reference o. At the same time, call the constructor of an input parameter.
Object o = assem. CreateInstance ("leleapplication2.example", false,
BindingFlags. ExactBinding,
Null, new Object [] {2}, null, null );

// Construct a late-bound SampleMethod of the Example class
MethodInfo m = assem. GetType ("leleapplication2.example"). GetMethod ("SampleMethod ");
// Call the SampleMethod method in the instantiated Example object o. The input parameter is 42.
Object ret = m. Invoke (o, new Object [] {42 });
Console. WriteLine ("SampleMethod returned {0}.", ret );

Console. WriteLine ("\ nAssembly entry point :");
Console. WriteLine (assem. EntryPoint );
}
}

 

 

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.