. Net reflection (2)

Source: Internet
Author: User

Http://service.ap-southeast-1.maxcompute.aliyun-inc.com.

Once a class object is obtained, the method listed in the above table can be called to call reflaction. The first example will check the information about the method obtained in the CSharpReflectionSamples. Reflect class. The first code is used to define the name of each method in the class. The second Code describes the method information. As shown below, we will use an array to save the information returned by the GetMethod () method. The MethodInfo class contains information about the method name. Whether it is virtual or not, it is visible.

Namespace CSharpReflectionSamples

{

Using System;

Using System. Reflection;

/// <Summary>

/// Summary description for Client.

/// </Summary>

Public class Client

{

Public static void Main ()

{

// The typeof operator and the GetType method

// Both return a Type object.

Type type1 = typeof (Reflect );

Reflect objTest = new Reflect (0 );

Type type2 = objTest. GetType ();

Console. WriteLine ("Type of objTest is {0}", type2 );

Console. WriteLine ();

// Pause

Console. ReadLine ();

// Reflect method information

MethodInfo [] minfo = type1.GetMethods ();

// Iterate through methods

Foreach (MethodInfo m in minfo)

{

Console. WriteLine (m );

}

Console. WriteLine ();

}

}

}

The next example shows how to dynamically obtain information about each constructor that an object may be exposed. Similar to the preceding example, we will return a ConstructorInfo object containing information about each ConstructorInfo constructer.

Namespace CSharpReflectionSamples

{

Using System;

Using System. Reflection;

/// <Summary>

/// Summary description for Client.

/// </Summary>

Public class Client

{

Public static void Main ()

{

// The typeof operator and the GetType method

// Both return a Type object.

Type type1 = typeof (Reflect );

Reflect objTest = new Reflect (0 );

Type type2 = objTest. GetType ();

Console. WriteLine ("Type of objTest is {0}", type2 );

Console. WriteLine ();

// Pause

Console. ReadLine ();

// Reflect constructors

ConstructorInfo [] cinfo = type1.GetConstructors ();

// Iterate through constructors

Foreach (ConstructorInfo c in cinfo)

{

Console. WriteLine (c );

}

}

}

}

The last part, perhaps the most exciting part in the reflection namespace, is to dynamically call class methods at runtime. There are two methods. First, we will create an array to store parameters. These parameters are used by the constructor to build objects. Second, a System. Object will confront the Object of the CreateInstance method. To get an example of an object. Finally, when we have object information, we can call any method using the MethodParm array. The following code is used:

Namespace CSharpReflectionSamples

{

Using System;

Using System. Reflection;

/// <Summary>

/// Summary description for Client.

/// </Summary>

Public class Client

{

Public static void Main ()

{

// The typeof operator and the GetType method

// Both return a Type object.

Type type1 = typeof (Reflect );

Reflect objTest = new Reflect (0 );

Type type2 = objTest. GetType ();

// Dynamic creation and invocation

// Instantiate the Reflect object, passing

// A value of 1 to the constructor

Object [] oConstructParms = new object [] {1 };

Object obj = Activator. CreateInstance (type1, oConstructParms );

// Invoke method of reflect object

Object [] oMethodParms = new object [] {17 };

Int intResult = (int) type1.InvokeMember ("AMethod", BindingFlags. Default |

BindingFlags. InvokeMethod, null, obj, oMethodParms );

Console. WriteLine ("Result of calling AMethod on {0} is {1 }",

Type1.Name, intResult );

// Pause

Console. ReadLine ();

}

}

}

This article describes the basics of. net Reflaction. In the next section, I will discuss further topics, such as dynamic publishing of intermediate languages, flag binding, and intermediate language principles.


Related Article

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.