C # dynamically load controls)

Source: Internet
Author: User

Good article
Controls play a very important role in rapid development. They can encapsulate a specific function for calls by programmers, more importantly, it can also implement plug-in development, greatly enhancing the flexibility and scalability of the software. There are also a lot of materials on the network, such as dynamically loading controls and dynamically calling class members.

Dynamic loading, the most basic use is the reflection mechanism. In the namespace of system. Reflection, there are a series of classes, interfaces, and structures for obtaining assembly information and class (type) information. The above may not be useful to anyone eager to dynamically load controls at all. So let's look at the following code. Maybe you can immediately implement what you want:

 

1. Load controls
Assembly = assembly. loadfrom (@ "C: controls. dll ");

2. Obtain the class (type)
Type type = assembly. GetType ("controls. usercontrol", false, true );

3. Set filter flag
Bindingflags bflags = bindingflags. declaredonly | bindingflags. Public | bindingflags. nonpublic | bindingflags. instance;

4. Call the constructor and obtain the object
Object OBJ = type. invokemember ("usercontrol", bflags | bindingflags. createinstance, null );

5. Convert the object type
System. Windows. Forms. Control control = (Control) OBJ;

6. Add the control to the form
This. Controls. Add (control );

 

========================================================== ========================================================== ====
The following describes some variables and methods used in the above section.

 

1. bindingflags Enumeration type
Bindingflags. instance: object instance
Bindingflags. Static: static member
Bindingflags. Public: contains public members in the search.
Bindingflags. nonpublic: indicates that a search can contain non-public members (Private Members and protected members)
Bindingflags. flattenhierarchy: a static member that can contain a hierarchy.
Bindingflags. ignorecase: Ignore the name case
Bindingflags. declaredonly: only searches for members declared on type, rather than simply inherited members.
Bindingflags. createinstance: Indicates calling the constructor. Ignore name. Invalid for other call flag

 

2. type. invokemember
Public object invokemember (
String name,
Bindingflags invokeattr,
Binder binder,
Object target,
Object [] ARGs
);

Parameters
Name
String, which contains the name of the constructor, method, attribute, or field member to be called. Or an empty string (""), indicating the default Member of the call.

Invokeattr
One-bit blocking is composed of one or more bindingflags that specify the search execution method. Access can be one of bindingflags, such as public, nonpublic, Private, invokemethod, and getfield. You do not need to specify the search type. If the search type is omitted, bindingflags. Public | bindingflags. instance will be applied.

Binder

A binder object that defines a set of attributes and enables binding. Binding may involve selecting overload methods, mandatory parameter types, and calling a member through reflection. -Or-if it is a null reference (nothing in Visual Basic), use defaultbinder.

Target calls the object of the specified member on it.

ARGs contains an array of parameters passed to the member to be called.

The returned value indicates the object returned by the called member.

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.