C # Use reflection to implement dynamic storage of theoretical knowledge on the winform Interface

Source: Internet
Author: User

1 Overview
In the process of developing database management software, personalized interface design is an essential part of a successful software product. Custom interface design is the layout of interface controls and the setting of control attributes, of course, you must consider the reasonable storage of interface controls. However, in order to meet the customer's needs, the traditional software interface design programmers need to develop a variety of user interfaces, which not only increases the software development cost, but also wastes a large amount of manpower and material resources, in fact, the interface functions are roughly the same, but the forms are different.
This paper proposes a feasible method to obtain interface control attributes during runtime Using Reflection technology, solves the problem of dynamic storage of interfaces and avoids repeated development of software interfaces, this improves the efficiency of software development.
2 reflection mechanism
C #. the reflection mechanism provided by Net, combined with the transmission of adaptive data parameters, can dynamically load and build the extension points in the application framework in Plug-in assembly mode, to implement dynamically scalable applications.
2.1 Basic Principles of reflection mechanism
Reflection is the way to obtain runtime type information in C #. Net. C #.. Net applications are composed of Assembly, Module, and class. Reflection provides a programming method, this allows programmers to obtain the relevant information when the program is running. For example, you can obtain the member attributes, member methods, events, and constructor of every type (including class, structure, Delegate, interface, and enumeration) in C #. Net at runtime. You can also obtain the name, qualifier, and parameters of each member.
2.2 reflection mechanism class hierarchy
The C #. Net reflection API is actually a type of data structure. Figure 1 shows a part of this structure-defined in the namespace System. Reflection. These classes can logically traverse the combination body and type information, and can start at any position in this hierarchy, depending on the specific design needs of the application.
2.3 functions of reflection mechanism
The application structure consists of application domains, assembly, module, type, and members. The Common Language Runtime Library loader manages application domains, including loading each assembly to the corresponding application domains and controlling the memory layout of the type hierarchies of each set.
The Assembly contains modules, while the module contains types and types, and reflection provides encapsulated assembly, module, and type objects. You can use reflection to dynamically create an instance of the type, bind the type to an existing object or obtain the type from the existing object, and then call the method of the type or access its fields and attributes.
Reflection is usually used for the following purposes: ① use Assembly to define and load an Assembly, load the modules listed in the Assembly list, locate a type from the Assembly, and create an instance of this type; ② use the Module to understand the Assembly containing the Module and the classes in the Module, and obtain all global methods or other specific non-Global methods defined on the Module; ③ use MethodInfo to understand the method name, return type, parameters, access modifiers (such as pulic or private), and implementation details (such as abstract or virtual; ④ use FiedInfo to understand the field name, access modifier, and implementation details (such as static), and obtain or set the field value; ⑤ use EventInfo to understand the event name, event handler data type, custom attributes, declaration type, and reflection type, and add or remove event handlers; ⑥ use PropertyInfo to understand the attribute name, data type, declaration type, reflection type, read-only or writable state.
3. Dynamic storage on the Software Interface
Dynamic Interface Storage refers to customizing the software interface through the toolbox and attribute window of the software. All the controls in the interface are the objects of the form. When the interface is stored, C # is used #. net reflection technology to dynamically obtain the attributes of bound objects and store them in XML files.
3.1 storage format of Dynamic Storage
You need to solve the storage format used for data. XML is currently becoming the preferred format for various databases, especially documents. Because of its ability to mark different fields, it makes data storage simpler and more dynamic, moreover, the XML file storage system is simple and easy to implement. Therefore, the storage format is in XML format.
3.2 How dynamic storage works
In C #. in the. Net FrameWork Development Platform, the form (that is, the software interface that the user sees) is a container-type control, you can add various controls (such as Button buttons, TextBox text boxes, and other container-type controls) to the form ). When designing the user interface of an application, software designers usually create a form statically, and add controls, set properties, and create Event Handlers based on functional requirements in the form, and add business logic to the form. In C #. in the. Net environment, the dynamic layout of each control object in the container control is determined by the attributes of the corresponding object. The object attributes include the attribute name and corresponding attribute values #. net reflection mechanism to dynamically capture the attributes of each object in the Custom interface, and save the attribute names and attribute values to the dynamic array respectively using the iterative method, and then use the attribute name as a tag, use attribute values as the TAG content and serialize the attribute data into XML format in a circular manner.
3.3 Implementation of Dynamic Storage
Because the interface is composed of many controls, the corresponding program design is also relatively complex. to simplify the description, here we take the software interface only the Button control as an example to illustrate the specific implementation of the interface dynamic storage.
To use the C # environment for dynamic storage of the software interface, you must first create a project, and then drag and drop a Button control on the form. In this case, the Button control becomes an object of the Form. Here, the form is named UIDesigner and the Button object is named button1. Then, you need to use the "Property Window" to set the corresponding properties for the Button object.
After the Button object on the software interface is customized, it can be dynamically stored. For a custom control on the interface, its method and attributes are uncertain for loading software. The C #. Net platform also provides methods for dynamically loading objects and calling objects, which are achieved through the C #. Net reflection technology. Implementation Method: create the ButtonObject class, Properties class, PropertyType class, propertytytypemanager class, And PropertyX class (including PropertyColor class, PropertyString class, PropertyBoolen class, PropertyFont class, And PropertySize class) in the project namespace). Its key class is Properties. First, add reference using System. Reflecttion, using System. Xml and using System. Xml. Serialization, and then create two dynamic arrays PropertyValues [] and PropertyInfos [] in the class. In this example, PropertyValues [] is used to save the attribute name/attribute value of button1, and PropertyInfos [] is used to save the attribute type/full name of button1. The class diagram used and its relationship 3 are shown.
Complete the program based on figure 3. In the load_UIDesigner event of the form UIDesigner code, define button1 as an object of the custom ButtonObject class and instantiate it, then, the interface control attributes can be extracted from the four modules in Figure 3: (1) → (2) → (3) → (3) → saved to the dynamic arrays of PropertyInfo and PropertyValue.
4 Conclusion
Use C #. net platform reflection technology, using the software interface dynamic storage design ideas, dynamic extraction of interface controls properties, stored in XML file format, the extension points in the application framework can be dynamically loaded and built using plug-in assembly to implement dynamically scalable applications. Practice has proved that this method can greatly improve the efficiency of software development and achieve the metadata information dynamic storage between application systems, with high scalability and code reusability.

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.