Current location - Recipe Complete Network - Complete cookbook of home-style dishes - VB.NET intelligent equipment semi-automatic initialization courseware
VB.NET intelligent equipment semi-automatic initialization courseware
I. Introduction

For NCF (short for Net) developers, there are usually only two ways to save application options:

1. Write the value of the option into the registry, but if all applications write a large number of values into the registry, the registry will eventually occupy too many system resources and affect the running efficiency of the system; This is why a lot of software must be reinstalled after the device is hard-started. According to the idea of "the program is as independent as possible from the system" in modern programming, this method is not recommended.

2. Saving the option values as initialization files can avoid the occupation of system resources to the maximum extent and improve the independence of program operation. Personally, this is a better plan. This method is in the full version of. Net, and can be implemented directly with Xml serialization classes. However, in the compact version of net designed specifically for smart mobile devices, saving and using program options is frustrating because XML serialization properties are not provided. Program developers must write/read files for each program option. This boring step will never be an interesting thing.

Second, the function overview

In this article, I will use the. Net to build a class that can automatically save/read initialization files. In this class, as long as the program author defines the internal member variables of the class according to the names of the program options in the class (this step still requires the programmer to code manually in the class, so this class is called semi-automatic initialization class), this class will automatically save/read the program options from the initialization file, and the programmer does not need to code the file part in a complicated way. Moreover, the construction of this class can also have a benefit: because the options of the application are stored in the class in the form of member variables, programmers can use the function of automatically listing variable members provided by VS to query the options of the initialization file. For example, write the string myappname = tobjapposition. General.appname like this. As far as I know, it is not fun to remember the exact characters of a large number of program options.

Thirdly, the premise analysis of program realization.

1. Requirements for initializing file contents

We first analyze and observe the contents of the standard windows initialization file win.ini:

[Windows] load = run = null port = none device = HP laserjet 6L PCL, PCL5EMS3, \ E5a18B631240425hplaserj [desktop] wallpaper = (none) Tile wallpaper = 1WallpaperStyle = 0.

The part enclosed in square brackets in the initialization file is called the section of the initialization file, and a series of program options with corresponding functions are organized under each section. E.g., the desktop part contains desktop wallpaper)/wallpaper style setting. In the initialization file, most program options can be saved as simple data types, such as string/number.

According to this demand, considering that. Net, using xml format can not only realize the functions of conventional windows initialization file, but also has the advantages of multi-tree structure organization. The initialization file designed in this paper is determined to use xml file format. And make the element definition of XML file according to the following format:

Net object defines the definition of' Net object' in the format used to save XML elements' classobjectname = objectname objecttype = datatype.

Definition of data content/data type' array At present, the array implemented by this class only supports one-dimensional array of string' ArrayObjectName = ObjectName ObjectType = DataType Length = ArraySizeElement Definition/ Array' If the Array is nothing, the format is as follows' Array Object Name = Object Type = Data Type Length = 0 Nothing/Array' Definition of Simple Object' int32, String, etc' Simple Object Object Name = Object Name = Data Type = Data Type.

Data content/data type' When SimpleObject represents the elements in the array, objectname represents the dimension of the array' ObjectName, ObjectType and Length'. Examples are as follows' For example, redim mai 32 test (7) as string' object name takes mai 32 test, ObjectType takes string [] and Length takes 8.

2. There is a function called reflection in. net, which can enumerate the types and stored values of member variables contained in specific types of objects. This function is often ignored by some ordinary programmers and is considered of little use. But in this paper, this function will become the core of constructing semi-automatic initialization objects, and we need this function to automatically save or read the variable types and values written in the initialization class into the initialization file.

Fourth, the program implementation core code comments

1. I named this semi-automatic initialization file class clsAPPOption. The internal structure and function of the inclusion process are as follows:

The contents in #Region "Option Structure Definition Used by Application" and #Region "Variable Declaration of Application Option" are classes and instances of classes organized by initialization option level, and each class represents an initialization segment of program option, which must be manually supplemented by end users according to actual option needs.

FnGetAppDirectory: get the running directory of the application.

SbInitialDefaultAPPOption: Sets the default initial value of program options (the code in this process needs to be modified manually according to actual needs).

Saves the program options in the class to the specified file. (In this process, some codes need to be modified manually according to actual needs. )

FnLoadAppOption: Read the option information of the application in the specified file and save it in the current class (some codes need to be modified manually according to actual needs in this process).

FnXMLElementToSimpleObject: Converts XMLElement into the simple object it represents, which is a basic net object, such as int32int 16.

FnXMLElementToClassObject: Converts XMLElement into the class object it represents.

FnXMLElementToArray: Converts XMLElement into the array it represents.

FnArrayToXML: Put the array into an XML file. Currently, only one-dimensional arrays are supported:

For example, dim string( 10) is used as the string.

FnClassObjectToxml: Converts class objects into representations of xml elements.

FnSimpleObjectToXML: Converts a simple object into an XML element representation.

Note: Among them, the functions of fnXMLElementToXXXX and fnXXXXToXML correspond to each other and are inverse functions.

2. The implementation of the program is very simple. FnClassObjectToXML performs reflection operations on the specified class.

Use the GetFields method of the class type to enumerate the member variable information in the class, and then call fnSimpleObjectToXML or fnArrayToXML according to the type of the member variable. At the end of the function, the class to be converted is returned as an XMLElement object.

TobjclassObjectType = ni _ objclassObject。 "gettype" gets the type of the class, which is convenient for reflection calling.

. Other codes

REM reflects all the values in the structure and stores them in xml objects.

For each tobj field information in tobjclassobjecttype. Getfieldsiftobj field information. Field type. Isarray = false then' is just a simple type. Get the values tobjxmlelement = fnsimpleobjecttoxml (tobjfield info.getvalue (ni _ objclassobject), _ ni _ objxmldocument, _ tobjfield info.name) tobjxmlclassobjectelement directly. If it is an array type, call the array method to get the value. At present, only one-dimensional array element tobjxmlelement = fnarraytoxml (tobjfield info.getvalue (ni _ objclassobject), ni _ objxmldocument, tobjfield info.name) is supported. Tobjfieldinfo. fieldtype.fullname)' Put the array object into the xml object with the structure tobjxmlclassobjectelement. appendchild(tobjxmlelement)end if。

The implementation of fnSimpleObjectToxml is also very simple. According to the definition of simple object established above, several elements of XML object to be generated by fnSimpleObjectToXML process can be obtained as follows: ObjectName has been obtained after being reflected by FNCLASSOObjectToXML, and it is provided when calling fnSimpleObjectToXML by parameter passing. The ObjectType data type can be obtained by using the method of ni_objSimpleObject. Gettype.fullname, ni _ objsimpleobject is the value of a simple object passed in when calling a function.

There is a small problem when writing fnArrayToXML procedure. Because the caller can't ask the elements of the array to be passed in one by one, the number and value of the elements of the array can't be obtained directly through the object reflection in the function. Fortunately, in the net reflection operation, the Invoke method can be used to call the function or attribute procedure in the original object, and the functions of the array sharing methods GetLength and GetValue can appropriately take the number of array elements and specify the subscript element values, and the problem will be solved immediately.

Use reflection to call the getLenght method of the array to get the size of the array. Only one-dimensional arrays are supported here. But for the initialization file, tobjxmlattribute = ni _ objxmldocument. Createattribute ("array length") tobjmethodinfo = tobjarraytype. Get method(“getlength ") is enough. ReDim taobj parameter(0)taobj parameter(0)= 0ti 32 tempa = tobjMethodInfo。 Call (ni_objArray, taobparameter)tobjXMLAttribute. Value = ti32TempatobjXMLElement。 Set attribute node (tobjxmlattribute) tobjxmlattribute = nothing' The elements in the array are used for ti 32 loop = 0 to ti 32 tempa- 1' The value of redimtaobjparametype (0) is obtained by using the reflected GetValue method. taobjParameterType(0)= GetType(Integer)tobjMethodInfo = tobjArrayType。 GetMethod("GetValue ",taobjParameterType)ReDim taobjParameter(0)taobjParameter(0)= ti 32 loopatobjtempa = tobjMethodInfo。 Invoke(ni_objArray, TaobjParameter)' puts the array elements into the xml object tobjxmlelement ta = fnsimpleobjecttoxml (tobjtempa, ni _ objxmldocument, ti32loop) tobjxmlelement. appendchild(tobjxmlelementa)next 32 loop。

There is basically no technical difficulty in the process of converting from XML to net objects. I just get the type of a simple object according to the ObjectType information contained in xml, and then directly call the function of net cast class to restore the value stored in the specified object in XML file to the original net object. The code is as follows:

According to the object type specified in the XMLelement structure element, Establish an object tobj type = type.gettype (tobjxmlsimpleobjectelement.getattribute ("objecttype")) tobjreturnsimpleobject = convert.changetype (tobjxmlsimpleobjectelement.innertext, tobj type, nothing).

For the method of converting Xml into arrays and objects, please refer to the source code attached to this article.

3. Initialize the call of the file class instance:

After the class definition is completed, you can define an instance of the initialization class in the global scope of the program.

Just define it in the module:

Public gobj option as new cls app option

When the program is started, the initialization file information can be read into the class. I put it in the load event of the main form.

Reader option gobjAppOption.fnLoadAppOption ()

You can save the value of the current semi-automatic initialization class to an xml file anywhere in the program.

I added the save code in the closed event of the main form:

GobjAppOption.fnSaveAppOption ()' option to save the application.

Anywhere in the program, you can call an instance of semi-automatic initialization file class to read/save the current values of program options:

You can make good use of the function of automatically listing members provided by VS to list the sample code messagebox of program options. Show (gobjappgeneric option. Astroshowfilefilter (0)) gobjappgeneric option of "sample code for saving options". astroideafilefilter (0) = "*。 zip "

Although this semi-automatic initialization file class still needs to add a small amount of code manually (in the part where the source code clearly indicates that it needs to be added manually), it is still an old saying that if the computer can do everything, the programmer will be laid off soon.