How to dynamically load controls and plug-in programming ideas (C #)

zhaozj2021-02-16  62

Keywords: dynamic loading, control, plugin

Controls, play a very important role in achieving rapid development, which can encapsulate a particular function, but more importantly, it also implements plug-in development, making the software flexibility, The expansion is greatly enhanced. On the network, there are also many dynamic load controls, dynamic call classes, and the like. Below, I will summarize the dynamic load control for your reference. (However, due to the limited level, it does not necessarily have a reference value, write it out, in order to sum up ourselves, in order to improve, on the other hand, I hope that each to see my shortcomings, give me a valuable advice) 1. Dynamic loading The control is dynamically loaded, and the most basic use is the reflex mechanism. There is a series of classes, interfaces, structures, and more on the Namespace of System.Reflection. It may be that a friend who is urgently imagined to realize the dynamic load control may not use it, then look at the following code, maybe you can realize you right away:

// Load control assembly assembly = askMBLY.LOADFROM (@ "c: /controls.dll"); // Get class (type) type type = askEMBLY.GETTYPE ("Controls.userControl", false, true); // Setting screening sign BindingFlags bflags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; // call the constructor and access to the object Object obj = type.InvokeMember ( "UserControl", bflags | BindingFlags.CreateInstance, null, null, NULL); // Transition the object transition type system.windows.forms.control c = (control) OBJ; // Add the control to the formimontrols.Add (C);

The following to some variables used in the above program, do some indicated 1, bindingflags, enumeration type BindingFlags.instance: Object instance BindingFlags.Static: Static member bindingFlags.public: Refers to the search in search BindingFlags.nonpublic : Refers to a non-public member (ie private member and protected member) bindingflags.flattenhierarchy: Indicates a static member BindingFlags.IgnoreCase that can contain hierarchical: indicating that the big lowercase bindingFlags.Declaredonly: only search for Type Members on the declaration, instead of searching for a simple inherited member bindingflags.createInstance: indicates call constructor. Ignore the Name. For other call logos, type 2, type.invokemememberpublic Object InvokeMember (String name, bindingflags invokeattr, binder binder, object target, object [] args); parameter name string, which contains constructor, method, attribute, or field member to call name. - or - empty string (""), indicating calling the default member. Invokeattr A bit shielded by one or more BindingFlags that specifies a search execution method. Access can be one of BindingFlags, such as Public, Nonpublic, Private, InvokeMethod, and GetField, etc. You don't need to specify the type of lookup. BindingFlags.Public | bindingflags.instance will be applied if the type of lookup is omitted. Binder a Binder object that defines a set of properties and enables binding, and binding may involve selecting a backup method, forcing parameter type, and a member of the reflex call. - or - If you are an empty reference (Nothing in Visual Basic), you will use the defaultbinder. Target To measure the Object of the specified member on it. Args contains arrays that pass parameters to be called. The return value represents the Object of the return value called member.

Second, the plugin programming passes the above code segment, we basically realize dynamic load controls. From this, I thought that now I mention a lot of plug-in development methods. By dynamic loading controls, is we not very convenient for software expansion? I don't know how the eclipse is implemented, but at least this method of dynamically load control implements a variant method of plug-in programming. Not? I make a function module into a control, and then get all the controls when the program start is a scan directory. When the menu is, the control is loaded into the form. I am in my matrix, but we have to do just a container window class to load the control. Of course, some agreement in advance, such as what are the methods for calling, and so on.

References:. 1 (C #) using the reflection to dynamically call the members of the class: lizanhong

2. To achieve the plug-in architecture of a C # program: Shawn Patrick Walcheske Translator: University of Electronic Science and Technology Summer masts

3. Building .NET Assemblies Dynamically Author: Govinda

转载请注明原文地址:https://www.9cbs.com/read-15878.html

New Post(0)