C # background and Analysis of Type reflection, late binding, and feature programming

Source: Internet
Author: User

All the features of any programming language are justified. Some features in C # may have been used without further understanding. However, if you know the background and principles of its use, it is easier to use. This article mainly analyzes type reflection, late binding, and feature programming in C. Why are we using these languages?

First, let's take a look at the needs of a simple project: programmers have developed many modules, each of which corresponds to the corresponding functions. Different users may need to use different modules. The modules are integrated with the system in the form of plug-ins, that is, the function of providing a user with a custom module.

For example, in a general library, the system includes the common instructor module, the dean module, the principal module, and the student module, these modules have the function of borrowing books (the number of books borrowed by each module is different). This general system may be used by many schools. Each school selects a module based on its own needs.

For the above requirement analysis, 1. Because these modules have the function of borrowing books, the first thing that comes to mind is to create an interface that contains a function for borrowing books, all modules must implement this interface.

2. implement these plug-in modules and compile them into assembly DLL files.

3. Because this is a general system, the choice of the module should be decided by the user. Therefore, we cannot compile all the module functions into the system in advance (late binding is useful ).

4. Selecting a module (DLL file) is equivalent to loading an assembly DLL (the concept of dynamic loading). At this time, the DLL is different from the DLL written in C/C ++, it is composed of Microsoft's intermediate language Il, which records

The metadata of all DLL information.

5. You need to call the specific borrow function in a specific DLL module. In this case, you need to obtain the corresponding class from the Dynamically Loaded DLL, instantiate the class, and call the member function of the class, this requires reflection.

6. Sometimes, in a large project, each module may have different sub-departments. Therefore, each module should have the basic information of these departments. At this time, when writing module functions, you can use the features in C # for programming.

Attaches additional information to the module, which will be compiled into the program set. Similarly, we can also reflect this information.

The following is an example of a simple code: using these features

Plug-in Interface Definition:

View code

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace commontypes {// define an interface public interface iappdll {void dothing ();} // custom feature class [attributeusage (attributetargets. class)] public sealed class carinfoattribute: system. attribute {public string name {Get; Set ;}}}

Develop third-party plug-ins according to the interface constraints defined above

View code

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; using system. windows. forms; using commontypes; namespace csharpdll {[carinfo (name = "this is a third-party production plug-in")] public class csharpmodule: iappdll {public void dothing () // implement the function {MessageBox. show ("CSHARP plug-in module is being called ");}}}

Call third-party plug-ins in form programs

View code

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. threading. tasks; using system. windows. forms; using system. reflection; using commontypes; namespace windowsformsapplication1 {public partial class form1: FORM {public form1 () {initializecomponent ();} private void opentoolstripmenuitem_click (Object sender, eventargs e) {// select a plug-in to load openfiledialog ofd = new openfiledialog (); If (OFD. showdialog () = dialogresult. OK) {If (! Loadmodule (OFD. filename) MessageBox. Show ("third-party plug-ins are not loaded successfully! ") ;}} Private bool loadmodule (string path) {bool findmodule = false; Assembly ASM = NULL; try {// load the Assembly ASM = assembly in the specified path. loadfrom (PATH);} catch (exception ex) {MessageBox. show (ex. message); return findmodule;} // obtain the variable VAR classtypes = from T in ASM that are compatible with all iappdll interfaces in the Assembly. gettypes () where T. isclass & (T. getinterface ("iappdll ")! = NULL) Select t; // create an object and call the method foreach (type T in classtypes) {findmodule = true; // use the late binding to create a type, forced conversion to interface type iappdll someapp = (iappdll) ASM. createinstance (T. fullname, true); someapp. dothing (); listbox1.items. add (T. fullname); // display the relevant information of the plug-in displaycarinfo (t);} return findmodule;} // display the plug-in information private void displaycarinfo (type T) {var info = from carasd in T. getcustomattributes (false) Where (carasd. getType () = typeof (carinfoattribute) Select carret; // display the attributes of the feature foreach (carinfoattribute CIA in info) {MessageBox. show (CIA. name );}}}}

 

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.