Once the class object is obtained, the method listed in the table above can be called to invoke Reflaction. The first example examines the information that gets the method in the Csharpreflectionsamples.reflect class. The first block of code is used to define the name of each method in the class, and the second piece of code will describe the method information. As shown below, we will use an array to hold the method information returned using the GetMethod () method. The MethodInfo class contains information as the name of the method, whether it is virtual or not, it is visible, and so on.
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 information about each constructor that the object might be exposed to dynamically. Similar to the above example, we will return an information ConstructorInfo object that contains each constructor.
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 of the reflection namespace, is the dynamic invocation of class methods at run time. There are two ways, first, that we will create an array to store the parameters that the constructor uses to build the object. Second, a System.Object object will confront the object of the CreateInstance method. To get an example of what you want. Finally, when we have the object's data, we are able to invoke any method that uses the Methodparm array. Here's the code:
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 ' {0} is {1} ')
Type1. Name, Intresult);
Pause
Console.ReadLine ();
}
}
}
This article describes the basics of. NET Reflaction, and in the next section I will discuss further topics, such as dynamic publishing of intermediate languages, banner bindings, and intermediate language principles.
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