This article from: http://apps.hi.baidu.com/share/detail/16193292
The author of this article (Guo Qinghua and Zhu zhanli), please read this article to respect the author's copyright.
Abstract: In order to achieve dynamic storage of software interfaces, a feasible method of using reflection technology to achieve dynamic storage of software interfaces is proposed in the C #. NET environment. This article introduces the advantages and features of reflection technology and XML, and provides the implementation and key of dynamic interface storage.Code. This method dynamically binds the properties of the interface control using the reflection technology to improveProgramCode reusability and flexibility.
Keywords: reflection technology; XML; software interface; serialization use C #. Net reflection technology to achieve dynamic storage of Software Interface
Guo qing-hua, Zhu Zhan-li
(School of Computer Science, Xi'an shiyou University, Xi'an 710065, China)
Abstract: In order to achieve dynamic storage of the software interface, the feasibility method is presented that using reflective technology achieves the dynamic storage of user interface (UI) in C #. net environments. introduce the advantages of reflection technology and XML, and it gives the concrete realization of dynamic storage and key code. this method uses reflection technology to dynamic bind the properties of UI controls and improve the program code reusability and flexibility.
Key words: Reflection techniques; XML; software interface; serialization
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 the 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 easier and 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, the attribute value is used as the TAG content, and the attribute data is serialized into XML format files by means of a loop. The implementation process 2 is shown in.
The author of this article (Guo Qinghua and Zhu zhanli), please read this article to respect the author's copyright.
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 #. NET 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.
References:
[1] Archer T. C # technical insider [M]. Hou Xiaoxia, Chai honghui, translated. Beijing: Tsinghua University Press, 2002.
[2] Richter J.. NET Framework programming (revised) [M]. Li Jianzhong, translated. Beijing: Tsinghua University Press, 2007.
[3] Watson K, Nagel C. C # entry-level classic [M]. Kang Bo, translated. Beijing: Tsinghua University Press, 2006.