C # reflection learning Summary

Source: Internet
Author: User

Reflection is a mechanism through which we can know an unknown type information. for example, there is an object A. This object is not defined by us. It may be captured through the network. It may be defined by generic type, but we want to know the type information of this object, I want to know the methods or attributes of this object. we even want to call the method of this object further. the key is that now we only know that it is an object. We do not know its type, and naturally do not know what methods it has and other information. what should we do at this moment? The reflection mechanism solves such a problem. Through the reflection mechanism, we can know the type information of unknown objects.
For example, we have a DLL file, and we want to call the class in it. now let's assume that the definition and quantity of classes in this DLL file are not fixed and change frequently. maybe you will add a class definition in this DLL one day. maybe you think this is okay. Now the key is that we need to call this DLL in another Assembly. This is what our program must be able to adapt to this DLL change, that is to say, even if the definition of the DLL file is changed, we do not need to change our Assembly. at this time, we will use an unknown DLL. what should we do? Similarly, the reflection mechanism helps us to achieve this through reflection.

The following is an example:

First, create a console application, add a class library model, and add a class people. CS in the class library as follows:

Namespace model {public class people {public string name {Get; set;} Public String sex {Get; set;} public int age {Get; set;} Public String getname () {return "my name is" + name;} Public String display () {return "My name is:" + name + "\ n my gender is: "+ sex +" \ n my age: "+ age +" \ n ";} public people (string name, string sex, int age) {This. name = Name; this. sex = sex; this. age = age ;}}}

Then, run the following code in the main function:

Static void main (string [] ARGs) {var Asssembly = assembly. load ("model"); // load the Assembly console. writeline ("assembly name: {0}", asssepolicy. fullname); console. writeline (); Type people = asssepolicy. getType ("model. people "); // obtain the type information. Enter the complete name object [] leleparms = new object [] {" yuan an cloud "," male ", 22 }; // The constructor parameter, which requires the overload of the constructor. If I want to write me like this. name = "yuan an cloud", how convenient it is,
// However, forced type conversion is required, such as VAR you = (model. People) Me, And then you. Name = "yuan an cloud ";
Object me = activator. createinstance (people, leleparms); // create an object methodinfo method = people. getmethod ("display"); // obtain information about the method console. writeline (method. invoke (Me, null); // call a method. The second parameter is a method parameter. It is an array of the object type and is set to null console when no parameter is required. readkey ();}

Running result:

Note:

In vs. net, there are many methods to dynamically call object constructors. First, use the createinstance () method of the activator class. This method is also used in remoting. It actually creates an object type locally or remotely, or gets a reference to an existing remote object. Its method signature is: public static object createinstance (type); (there are other overload methods). Note that its return value is object. The return value of msdn is described as follows: references the newly created object.
The second is to use the createinstance () method of the Assembly class (). The method name is the same as the previous one, but it is not a static method. Assembly is in the system. Reflection namespace. Method Signature: Public object createinstance (type); (there are also other overload methods) the returned value is still object, and msdn describes the returned value as an instance of this type of object, its Culture, parameters, federated programs, and activation attributes are set to null reference (nothing in Visual Basic), and bindingflags is set to public or instance, or null reference (nothing) (If typename is not found ).

Of course, there are other methods. For example, after obtaining method information through methodinfo, determine whether to construct a function based on the isconstructor attribute, obtain parameters based on the getparamters () method, and finally call the method through the invoke () method, and so on ....... You can refer to msdn.

Problem:

The preceding call method display is implemented as follows:

Methodinfo method = people. getmethod ("display"); // gets information about a method.
Console. writeline (method. Invoke (Me, null); // call a method. The second parameter is the method parameter. It is an array of the object type and is set to null when no parameter is required.

What if I want to write me. Display () in this way? Use forced type conversion:

VaR you = (model. People) Me;
Console. writeline (you. Display ());

 

 

 

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.