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.