The params () array is the parameter content table of the constructor, which is the same because there is no parameters, we use the redim -1 syntax.
The Invoke method performs a constructor and creates an object instance.
Now we return to the first implementation method, change the code, will
DIM T as type = gettype (myclasstest)
Change to
DIM T as type = type.gettype ("vbapplication.myclasstest")
The result of run has not changed, this is to say that we have implemented an object from a string! However, the use of the GetType method is limited, and let's talk about us. Now we can realize our desire: dynamically create controls. Through the above knowledge, we easily write a subroutine that dynamically creates a window control:
Private Function CreateNewControls (ByVal targetControls As Control.ControlCollection, ByVal ctlName As String, ByVal ctlType 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 = CTLOCATION
TargetControls.Add (Tocreate)
Return Tocreate
END FUNCTION
The longer statement contains all the contents in the previous example. If you write with C #, you can write it.
TOCREATE = (Control) System.activator.createInstance (CTLTYPE);
We change the event process of the button:
DIM C As Control = Me.createNewControls1 (Me.Controls, "Control1", Gettype (Checkbox), New Size (168, 40), New Point (64, 176))
C.Text = "New Creation"
Now, click the button to see a new Checkbox appear on the window, the title is New Creation, and if the event process is written, you can add an event response for the new control.
It seems that everything is achieved? Note that getType (Checkbox) or the literal representation of the class name, cannot reach the function of creation of objects with strings. If we change this to Type.gettype ("System.Windows.Forms.Checkbox") Well, test, huh, huh, I have fallen. Why is this this?
The Type.getType () method is limited to the type of strings or types in the Corlib or the types of the project. If it is from the external assembly, you need to add the name of the assembly. The Windows.Forms assembly is a public assembly, which is located in the program set cache, which can be implemented inside .NET FRAMWORK execution. So this assembly has a different version. In order to determine the version of the version, we must not only provide the name of the assembly, but also provide the version and strong name of the assembly. According to this idea, on the .net Framework 1.1 I use these a written Type.GetType ( "System.Windows.Forms.CheckBox, System.Windows.Forms, Version = 1.0.5000.0, Culture = neutral, PublicKeyToken = b77a5c561934e089 "). There is no problem now. The problem is how do we get the version and strong name of the Windows.forms assembly used? You can use the syntax such as gettype (checkbox). Once this information is obtained, we can use this information for any other control because 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, change the button's event process to: 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 = Newral, PublickeyToken = B77A5C561934E089 "), New Size (168, 40), New Point (64, 176))
C.Text = "New Creation"
Catch exception
MSGBOX (ex.Message)
END TRY
Now as long as you type "button" in TextBox, press the button, a new button is generated! If you entered Checkbox, a check box will be generated. No matter how hard is the user, the control can "create" on demand ". The reflective mechanism has many uses in .NET, it is said that functions such as class references and virtual constructors in delphi.net are used for .NET FRAMWORK is achieved by means of reflection and system.Type types, making this tool will give Your program has a lot of color.