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

zhaozj2021-02-16  91

Control, play a very important role in achieving rapid development, it can encapsulate a particular function, but more importantly, it can also implement plug-in development, so that 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 Controls ?????? Dynamic loading, the most basic use is the reflection 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 the filter mark ??????????? bindingflags bflags = bindingFlags.Declaredonly | bindingflags.public ??????????????????????????????????????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????? | bindingflags.nonpublic | bindingflags.instance; ?????????? ? // Call the constructor and get the object ??????????? Object obj = type.invokememember ("UserControl", bflags | ????????????????? ??????????????????????????????????????????? ?????? bindingflags.createInstance, null, null, null; ??????????? // convert the object transition type ?????????? system.windows.Forms .Control c = (control) Obj; ??????????? // Add control to the form ????????????????????? THISCONTROLS.ADD (C);

The following to some variables used above, the method is made 1, bindingflags, enumeration type ??? bindingflags.instance ?????????????????????: object Instance ??? bindingflags.static ????????????????????????: static member ???? bindingflags.public ????????? ??????????????: Refers to the search in the search, including a public member ??? bindingflags.nonpublic ???????????????: indicates in the search Contains non-public members (ie private members and protected members) ??? bindingflags.flattenhierarchy ????: Indicate a static member on hierarchical structure ????????????????????? ???: Indicates the big lowercase of Name ??? bindingflags.declaredonly ?????????: Search only members of the declaration on Type instead of searchable members of the simple inherited ??? bindingflags.createInstance ?? ????: Indicates the call constructor. Ignore the Name. Type.InvokemeMberpublic Object InvokeMemberpublic Object InvokeMember (?? String name, ?? bindingflags invokeattr, ?? binder binder, ?? Object target, ?? Object [] args); parameter ?????? Name? ???????? String, which contains the name of constructor, method, attribute, or field member to be called. ????????? - or - ????????? Empty string (""), indicating calling the default member. ????? INVOKEATTR ??????????? One bit shields, composed of 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. If the lookup type is omitted, bindingflags.public | bindingflags.instance will be applied. ?????? binder ?????? ?? A Binder object defines a set of properties and enable binding, and binding may involve selecting a backup method, • Force parameter type and by reflecting a member. ??? - or -? "If you are an empty reference (Nothing in Visual Basic), use the defaultbinder. ???? Target ???????? To measure the Object of the designated member. ??? args ??????? Contains an array that passes the parameters of the member to be called. ?? Return value ??????? indicates the Object of the return value that is called member.

Second, plug-in programming ???????? Through the above code segment, we basically realize dynamically 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 dynamic invocation of class members ??: ?? lizanhong

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

3. Building .NET Assemblies Dynamically ?? Author:? Govinda?

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

New Post(0)