When using .NET created, Metadata and Code (CODE) are stored in a "self-contained" unit, which is called an assembly. We can access this information during the program. There is such a Class ---- Assembly in System.Reflection, we can load a fitting with it. The method is as follows: askMBLY ASSM = Assembly.LoadFrom (filename); where FileName is the file name (with path) of the assembly to load. Next, we can get information in the assembly by using Info Classes provided in System.Reflection. Let's take a look at these Info Classes: MethodInfo gets information about a "member function" and provides access to this "member function" metadata. ParameterInfo gets information about a "parameter" and provides access to this "parameter" metadata. ConstructorInfo Gets information about a "constructor" and provides access to this "constructor" metadata. PropertyInfo gets information about a "property" and provides access to this Properties "metadata. FieldInfo gets information about a "data member" and provides access to this "data member" metadata. EventInfo Gets an "event" information and provides access to this "event" metadata. The access operations of these classes (except ParameterInfo) listed above are completed through a TYPE object. For example, we have to get a "member function" of a fitting, you have to do this: system.Reflection.Assembly ass = system.reflection.Assembly.LoadFrom (filename); type [] tp = ass.gettypes (); system.reflection. MI = TP [0] .getMethods (); Using the same approach we can also get other information, as follows: Get "constructor" information: system.reflection.constructorinfo [] Ci = TP [0] .getconstructors (); Obtain "attribute" information: system.reflection.propertyInfo [] pi = TP [0] .GetProperties (); get "data member" information: system.reflection.fieldInfo [] FI = TP [0] .Getfields ); Obtain "event" information: system.reflection.eventinfo [] EI = TP [0] .getevents (); In addition, we can get the parameter information of "Member Functions" and "Constructor" through the ParameterInfo class, as follows: Get the parameter information of "Member Function": system.reflection.ParameterInfo [] Pi = Mi [0] .GetParameters (); Get parameter information for "constructor": system.reflection.ParameterInfo [] Pi = Ci [0]. GetParameters (); ParameterInfo class has two important properties: name and parametertype. Through them we can get the name and data type of "parameter".
Since .NET encapsulates Class's information in the form of "metadata" in the program or components, it provides a series of methods that can get "metadata", so we can run access to this information during operation. We often encounter such a situation when writing programs: there is a lot of calculations in the program, and this calculation method has a lot of calculation, we have to consider it very comprehensive when writing a program, will considering. But this is also very laborious, because we can't predict how the program is compiled, what kind of calculation method will also occur. If the calculation method is to deliver to the customer, our newly proposed us have to write new computing methods, then recompile, and then hand it over. This is quite troubles, and only for such a short program, it is necessary to recompile the entire project. It seems that the cost is quite big. Use some of the method in System.Reflection in MS.NET to help us solve the above problems. First, when we encounter the above problems, we must first analyze it. What parameters do you need? What is the common parameters in different computing methods? Is the unique parameter in different computing modes can be calculated by a common parameter, or obtained by other methods? After the analysis is finished, the common parameters available are extracted. Next, we can write a calculation method. Each of this calculation is written into a method in a DLL in a class and compiles it into a DLL file. In MS.NET, the format of the class is to be killed, that is, the names of the Namespace and Class written, the method name in the class must also be the same. Moreover, the parameters of the method are the common parameters mentioned above. Then, put the compiled DLL file in the same folder, and it can be published along with the program. In the program, the different calculation methods to be used should be handled: give each calculation mode (the customer can understand), then put these names in the text property of the drop-down list box, and will correspond to the corresponding DLL file The name is placed in the Value property of the drop-down list box. In this way, the user selects a different calculation method to call the calculation method in different DLL files. Here is a simple example: I put the name of the calculated method and the DLL file name in an XML file, and the program is loaded to the drop-down list box.