How to create a control dynamically from a string on. Net

Source: Internet
Author: User
Tags contains reflection
Create | dynamic | controls | strings

Dynamically creating a control on. NET with a string is achieved by reflection.

First, use the System.Type.GetType method to get the type instance of the control specified in the string.

Here you need to pay attention to the syntax of this string, according to the MSDN explanation:

    1. hiding by name and signature takes into account all parts of the signature, including custom modifiers, return types, parameter types, tags, and unmanaged calling conventions. This is binary comparison.
    2. For reflection, properties and events are hidden by name and signature. If there are properties in the base class that have both a get accessor and a set accessor, but only a get accessor in the derived class, the derived class property hides the base class property and you will not be able to access the base class's setup program.
    3. Custom attributes are not part of a common type system.

Do not search the array or COM types unless they have been loaded into the available class tables.

TypeName can be a simple type name, a type name that contains a namespace, or a complex name that contains an assembly name specification.

If TypeName contains only the name of Type, this method searches in the assembly of the calling object first and then in the mscorlib.dll assembly. If TypeName is fully qualified with a partial or full assembly name, this method searches in the specified assembly.

AssemblyQualifiedName can return a fully qualified type name, including nested types and assembly names. All compilers that support the common language runtime emit a simple name for a nested class, and when queried, reflection constructs a mangled name according to the following conventions.

For example, the fully qualified name of a class might resemble the following form:

topnamespace.subnamespace.containingclass+nestedclass,myassembly

But using Type.GetType ("System.Windows.Forms.TextBox") directly to get the type is null. This is because the Windows.Forms assembly is a public assembly that is in the assembly cache, and the assembly has a different version, and in order to determine which version to use, we will not only provide the name of the assembly, but also the version and strong name of the assembly. In this way, on the use of the. NET Framework 1.1, write this sentence as Type.GetType ("System.Windows.Forms.CheckBox, System.Windows.Forms, version= 1.0.5000.0, Culture=neutral, publickeytoken=b77a5c561934e089 "). There is no problem with running now. The question is how do we get the version and strong name of the Windows.Forms assembly we use? You can use GetType (CheckBox). AssemblyQualifiedName Such syntax, once this information is available, we can use that information for any other control, since they all come from the same version of the Windows.Forms assembly.

Using the method mentioned above, you can now use the System.Activator.CreateInstance method to create a TextBox control:

public static void CreateControl (string controltype, form form, int positionx, int positiony)
{
Try
{
String assemblyqualifiedname = typeof (System.Windows.Forms.Form). AssemblyQualifiedName;
String assemblyinformation = Assemblyqualifiedname.substring (Assemblyqualifiedname.indexof (","));
Type ty = Type.GetType (ControlType + assemblyinformation);
Control NewControl = (Control) System.Activator.CreateInstance (Ty);
Form. SuspendLayout ();
Newcontrol.location = new System.Drawing.Point (Positionx, positiony);
NewControl.Name = Ty. Name + form. Controls.Count.ToString ();
Form. Controls.Add (NewControl);
Form. ResumeLayout ();
}
catch (Exception ex)
{
Throw ex;
}
}

Call: CreateControl ("System.Windows.Forms.TextBox", this, 10, 10);




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.