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

Source: Internet
Author: User
Tags constructor contains reference reflection
Programming | dynamic | loading | Control keywords: dynamic loading, controls, Plug-ins

Control, it plays a very important role in realizing rapid development, it can encapsulate a certain function, can be invoked by the programmer, and more importantly, it can realize plug-in development, so that the flexibility and extensibility of software are greatly enhanced. On the network, there are also a lot of dynamic load control, dynamic invocation of class members and so on data. Below, I will dynamically load the control to summarize, for everyone's reference. (However, due to my limited level, there is no reference value, write out on the one hand is to summarize their own, in order to improve, on the other hand also hope that friends see my shortcomings, give me valuable advice)
One, dynamic load control
Dynamic loading, the most basic use is the reflection mechanism. Under the namespace of System.Reflection, there are a series of classes, interfaces and structures for obtaining assembly information, class (type) information. Perhaps the above words may be of little use to a friend eager to implement dynamic load controls, so look at the code below, and you may be able to achieve what you want immediately:

Load control
Assembly Assembly = Assembly.LoadFrom (@ "C:\Controls.dll");
Get Class (Type)
Type type = assembly. GetType ("Controls.usercontrol", false,true);
Set Filter Flags
BindingFlags bflags = BindingFlags.DeclaredOnly | BindingFlags.Public
| BindingFlags.NonPublic | BindingFlags.Instance;
Call the constructor and get the object
Object obj = type. InvokeMember ("UserControl", Bflags |
Bindingflags.createinstance, NULL, NULL, NULL);
Convert an object to a type
System.Windows.Forms.Control C = (control) obj;
To add a control to a form
This. Controls.Add (c);


Some of the variables and methods used in the above program section are described below.
1, BindingFlags, enumeration type
BindingFlags.Instance: object instance
BindingFlags.Static: Static members
BindingFlags.Public: Refers to the inclusion of public members in the search
BindingFlags.NonPublic: means that you can include non-public members (that is, private members and protected members) in the search
BindingFlags.FlattenHierarchy: Refers to static members that can contain hierarchies
Bindingflags.ignorecase: Indicates that the case of name is ignored
BindingFlags.DeclaredOnly: Only the members declared on the Type are searched, not the members that are simply inherited
Bindingflags.createinstance: Represents the call constructor. Ignore name. Invalid for other invocation flags

2, Type.InvokeMember
public Object InvokeMember (
String name,
BindingFlags invokeattr,
Binder Binder,
Object Target,
Object[] Args
);
Parameters
Name
String that contains the name of the constructor, method, property, or field member to invoke.
Or
An empty string ("") that represents calling the default member.
Invokeattr
A bitmask consisting of one or more BindingFlags that specify how the search executes. Access can be one of the BindingFlags, such as public, nonpublic, Private, InvokeMethod, and GetField. You do not need to specify a lookup type. If the lookup type is omitted, the BindingFlags.Public is applied | BindingFlags.Instance.
Binder
A Binder object that defines a set of properties and enables binding, which may involve the selection of overloaded methods, coercion of parameter types, and invocation of members by reflection. -or-if it is a null reference (Nothing in Visual Basic), use DefaultBinder.
Target
The Object on which to invoke the specified member.
Args
An array that contains the arguments that are passed to the member to be invoked.
return value
Object that represents the return value of the invoked member.

Second, plug-in programming
Through the code snippet above, we basically implement the dynamic load control. From this I think of the web now mentioned a lot of plug-in development methods. By dynamically loading the controls, isn't it convenient for us to extend the functionality for the software? I don't know how eclipse is implemented, but at least this dynamic load control method implements a workaround for plug-in programming. Isn't it? I put a function module into a control, and then the program started is scan the directory, you can get all the controls, when the Click menu is to load the control into the form. I'm in the parent program, all we have to do is to have a container window class to load the control. Of course, there must be some agreement beforehand, for example, what methods the control has to invoke, and so on.


Resources:

1. (C #) dynamic invocation of class member authors using reflection: Lizanhong

2. Implement plug-in architecture in C # programs Author: Shawn Patrick walcheske Translator: The summer mast

3. Building. NET assemblies Dynamically author: Govinda




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.