Implementation runtime creates objects dynamically from strings in. Net

Source: Internet
Author: User
Tags reflection
Create | dynamic | object | The string params () array is the parameter content table of the constructor method, and also because there are no arguments, we use the ReDim-1 syntax.
  
The Invoke method executes the construction method and creates an object instance.
  
Now let's go back to the first implementation and change the code to
  
Dim T as Type = GetType (myclasstest)
  
To
  
Dim T as Type = Type.GetType ("Vbapplication.myclasstest")
  
The results of the run did not change, that is to say, we implemented the object from the string creation! However, there are restrictions on the use of the GetType method, and we'll talk about it later. Now we can achieve our wish: dynamically creating controls. With the knowledge above, it is easy to write a subroutine that dynamically creates a window control:
  
Private Function Createnewcontrols (ByVal targetcontrols as Control.controlcollection, ByVal ctlname as String, ByVal CTLT Ype as Type, ByVal ctlsize as Drawing.size, ByVal ctllocation as Drawing.point) as control
  
Dim Tocreate as Control
  
Tocreate = CType (System.Activator.CreateInstance (Ctltype), control)
  
Tocreate.name = Ctlname
  
Tocreate.size = Ctlsize
  
Tocreate.location = Ctllocation
  
Targetcontrols.add (tocreate)
  
return tocreate
  
End Function
  
The longer sentence contains all the contents of the previous example. If you write in C #, you can write
  
Tocreate = (Control) System.Activator.CreateInstance (Ctltype);
  
We change the button's event procedure to:
  
Dim C As Control = ME.CREATENEWCONTROLS1 (Me.Controls, "Control1", GetType (CheckBox), New Size (in), New Point (64, 176) )
  
C.text = "New creation"
  
Now, by clicking the button, you can see a new checkbox appearing on the window, titled New Creation, and, if you write an event procedure, you can add an event response for the newly created control.
  
Looks like everything's up to the end. Note that the GetType (CheckBox) or the literal representation of the class name does not achieve the ability to create objects with strings. If we change this sentence to Type.GetType ("System.Windows.Forms.CheckBox"), can we? Well, try it, hehe, it's a mistake. Why is that? The Type.GetType () method obtains the type from the string to be limited to the type in corlib or the type within the project, and the name of the assembly is required if it is from an external assembly. The Windows.Forms assembly is a public assembly that is located in the assembly cache and can be implemented side by side within the. NET Framwork. So this 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. NET Framework 1.1 that I use, I 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. Now you can play a fun, put a text box to the window, such as called TextBox1, the button's event procedure to read:
  
Try
  
Dim C As Control = ME.CREATENEWCONTROLS1 (Me.Controls, "Control1", Type.GetType ("System.Windows.Forms." & TextBox1.Text & ", System.Windows.Forms, version=1.0.5000.0, Culture=neutral, publickeytoken=b77a5c561934e089"), New Size (in), New Point (64, 176))
  
C.text = "New creation"
  
Catch ex as Exception
  
MsgBox (ex. Message)
  
End Try
  
Now just type in the TextBox1 type "button", press the button, a new button produced! If you enter a checkbox, a check box is generated. Now, no matter how difficult the user is, the control can be "created on demand" correctly. The reflection mechanism has many uses in. NET, and it is said that features such as class references and virtual constructors in Delphi.NET are used for. NET Framwork with the help of reflection and System.Type types, and the use of this sharp weapon will add a lot to your program.




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.