Use reflection to dynamically create instances and Invoke methods . NET is a powerful feature of its ability to access the application's metadata through a process called reflection (reflection). Simply put, reflection is the ability to query type information at run time. The. NET Reflection API is actually a set of classes defined in the System.Reflection namespace. These classes enable you to view accessories and type information in a logical way. I am not here to explain the basic principles of reflection, but for some of the basic use of reflection to do the introduction. There are some theoretical things about the definition of reflection see other articles on the Web or MSDN. Here I only talk about three applications: one is to create an instance with reflection, two is to invoke the instance with reflection, and the third is to invoke the property of the instance with reflection. These three are the most basic uses and uses of reflection, other uses such as invoking the characteristics of the instance, delegates, and so on, and the three similar, ask the reader to learn by themselves. learning to reflect a lot of basic knowledge, here is not ready to talk about these things, just come to the point of the three uses. before this, I would like to say that the type of this class, not interested readers can not read this paragraph. The core of reflection is the class of abstract System.Type. This class is the root of all reflection operations and represents each type in the system. This class is the primary way to access metadata as a channel leading to the reflection API. Members of the type class are used to get information about type declarations. This information includes all constructors, methods, fields, attributes, and events defined for the type, as well as the module and accessories where the class resides. Type objects can represent any of the following types: classes, value types, arrays, interfaces, pointers, and enumerations. &nbSp The following program is a simple project called Reflectionexample, It includes MainClass.cs and InstanceClass.cs two files, mainclass which use strings to create Instanceclass classes and Invoke properties and methods in Instanceclass, including calling the default constructor and calling overloaded constructors , calling parameterless member functions and member functions with parameters Four methods, there is also a Returnstring property, the program is as follows. InstanceClass.cs file using System; namespace Reflectionexample { ///<summary> A summary description of the ///Instanceclass. ///</summary> Public Class Instanceclass { private String returnstring; public string Returnstring { get {return returnstring;} set {returnstring = value;} } Public Instanceclass () { // //TODO: Add constructor code to this program this.returnstring = "creat object without Parameter"; } public Instanceclass (String str) { this.returnstring = str; } public void Functionwithoutparameter () { Console.WriteLine ("Function without Parameter"); } public void Functionwithparameter ( String str) { Console.WriteLine (str); } } MainClass.cs file using System; Using System.Reflection; namespace Reflectionexample { ///<summary>  A summary description of the; ///Class1. ///</summary> Class MainClass { private Type type = NULL; ///<summary> ///application's main entry point. ///</summary> [STAThread] static void Main (string[) args) { // /TODO: Add program code to start application // Type type = typeof (Instanceclass);//Use typeof to get Instanceclass types. MainClass mainclass = new MainClass (type);//Initialize MainClass class Mainclass.getobjectmethod ()//calling function without parameters Mainclass.getobjectmethod ("function with Parameter");//Call function with parameters Mainclass.getobjectproperty ()//Call the default constructor to initialize the Returnstring property in Instanceclass Mainclass.getobjectproperty ("Creat object with Parameter");//Invoke overloaded constructor to initialize returnstring properties in Instanceclass } public MainClass (type type) { & nbsp; This.type = type; } public void Getobjectmethod () { Try { //activator.createinstance () invokes the default constructor of the class to create an instance object o = activator.createinstance (type); //using the MethodInfo class to get the member functions from the specified class MethodInfo mi = type. GetMethod ("Functionwithoutparameter"); //Call this member function mi. Invoke (o, null); } catch (Exception ex) { Console.WriteLine (ex. message); }   Finally { Console.ReadLine (); } } public void Getobjectmethod (string str) { Try { object o = Activator.CreateInstance (type); //using the MethodInfo class to obtain a member function that meets the criteria from the specified class MethodInfo mi = type. GetMethod ("Functionwithparameter", bindingflags.public| BindingFlags.Instance, NULL, new type[]{typeof ( string)}, NULL); mi. Invoke (o, new object[]{str}); } catch (Exception ex) {
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.