C # reflection,

Source: Internet
Author: User

C # reflection,

. Net is often used to dynamically load DLL, and DLL may contain various parameters, methods, forms. How to call to dynamically load these parameters, methods, and forms?

 

In C #, we need to use reflection. First, we need to understand the relationships between several classes in the following namespace:

System. Reflection namespace

(1) AppDomain: application domain, which can be understood as a logical container of a group of assemblies

(2) Assembly: Assembly class

(3) Module: Module class

(4) Type: Use reflection to obtain the core class of the Type information.

An AppDomain can contain N assemblies, an Assembly can contain N modules, and a Module can contain N types.

 

 

In a program, you can use the Load, LoadFrom, LoadFile, and LoadWithPartialName methods to dynamically Load an assembly.

The differences between Assembly. LoadFile and Assembly. LoadFrom are highlighted below.

1. Assembly. loadFile only loads the corresponding dll file, such as Assembly. loadFile ("abc. dll "), load abc. dll, if abc. def. def. the dll is not loaded.

Assembly. LoadFrom is different. It loads the dll file and other dll referenced by it. For example, in the above example, def. dll is also loaded.

2. Use Assembly. when LoadFrom loads an Assembly, it first checks whether the Assembly with the same name has been loaded before, such as abc. the dll has two versions (version 1 is in directory 1 and Version 2 is in directory 2). Version 1 is loaded at the beginning of the program, and Assembly is used. loadFrom ("2 \ abc. dll ") when loading version 2, it cannot be loaded, but returns version 1. Assembly. LoadFile does not perform this check. For example, if the preceding example is replaced by Assembly. LoadFile, version 2 can be correctly loaded.

LoadFile: load the content of the Assembly file in the specified path.

LoadFrom: loads the content of the Assembly file according to the Assembly file name.

 

Call method code:

For example, the DLL to be reflected is named WindowsFormsApplication2.dll, which contains a From1 form and a Class1 class. The class has two methods: a and B. The a method must pass an Int parameter, the returned result is a parameter + 1. If Method B does not need to pass a parameter, the returned result is 1. Both methods are static methods.

Call Method B without passing parameters:

Assembly outerAsm = Assembly. loadFrom (@ "urPath \ WindowsFormsApplication2.dll"); Type type = outerAsm. getType ("WindowsFormsApplication2.Class1"); // call type MethodInfo method = type. getMethod ("B"); // call method // The object obj = string parameter is not required. empty; string value = method. invoke (obj, null ). toString (); // value = 1

To call method a, you must pass the following parameters:

Assembly outerAsm = Assembly. loadFrom (@ "urPath \ WindowsFormsApplication2.dll"); Type type = outerAsm. getType ("WindowsFormsApplication2.Class1"); // call type MethodInfo method = type. getMethod ("a"); // call method // if you need to pass the parameter object [] paramertors = new object [] {2016}; // string value = method in the parameter set. invoke (null, paramertors ). toString (); // Invoke call method value = 2017

Call the form and load it out:

Assembly outerAsm = Assembly. loadFrom (@ "urPath \ WindowsFormsApplication2.dll"); Type type = outerAsm. getType ("WindowsFormsApplication2.Class1"); // call Type // call the Form Type outerForm = outerAsm in DLL. getType ("WindowsFormsApplication2.Form1", false); // locate the specified window (Activator. createInstance (outerForm) as Form ). show (); // convert to form class, display

 

Reference: http://www.cnblogs.com/mumupudding/p/4607400.html

 

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.