To implement the runtime to create an object dynamically from a string

Source: Internet
Author: User
Tags constructor implement reflection
Create | dynamic | object | Strings at run time to specify the type of creation of an object, or even to create the desired object with a string representing the name of the type, the. NET Framwork reflection mechanism gives us a way to solve the problem. Here, if you just want to create a generic object, we can do it through system.activator, and the more complicated we can do by getting the constructor.

Reflection reflection is an important mechanism in. NET, with reflection, you can get members of each type in. NET (including classes, structs, delegates, interfaces, enumerations) at run time, including methods, properties, events, and constructors, as well as the names, qualifiers, and parameters of each member, with reflection , you can be knowledgeable about each type. If you get the information for the constructor, you can create the object directly, even if the type of the object is not known at compile time.

<summary>
Createnewcontrols dynamically generates a control based on the name of the space, type string, size, position
</summary>
<param name= "Targetcontrol" > Controls loaded into the container </param>
<param name= "Ctlname" > Generated control Instance name </param>
<param name= "Ctltype" > Generated control type strings (TextBox, button, etc.) </param>
<param name= "Ctlsize" > Control size </param>
<param name= "ctllocation" > Control position </param>
<returns> the generated control instance </returns>
Private control Createnewcontrols (control.controlcollection targetcontrol,string ctlname,type Ctltype, System.Drawing.Size ctlsize,system.drawing.point ctllocation)
{
Control tocreate;
Tocreate = (Control) System.Activator.CreateInstance (Ctltype);
Tocreate.name = Ctlname;
Tocreate.size = ctlsize;
Tocreate.location = ctllocation;
Targetcontrol.add (tocreate);

return tocreate;
}




Size cbsize = new size (160,40);

Point cbpoint = new Point (64,206);

Control C1 = Createnewcontrols (this. Controls, "Control1", Type.GetType ("System.Windows.Forms.CheckBox, System.Windows.Forms, version=1.0.5000.0, Culture=neutral, publickeytoken=b77a5c561934e089 "), Cbsize,cbpoint);

C1. Text = "Check box";



. NE tframework 1.1, Type.GetType ("System.Windows.Forms.CheckBox, system.windows.forms,version=1.0.5000.0,culture= Neutral, publickeytoken=b77a5c561934e089 ").

How do we get the version and strong name of the Windows.form assembly used? 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.


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.