[RESOLVED] [2005] .NET equivalent of COM's CreateObject
How do you create an object instance if all you have is the name of the class?
I want to add controls (derived from webcontrols) onto a webpage at runtime. Information is stored in a database, so all I know is the class name of the control, ie
Code:
Public Class wssList
Inherits System.Web.UI.WebControls.BulletedList
Public Sub New()
Me.Items.Add("Test 1")
Me.Items.Add("Test 2")
End Sub
End Class
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ctl As System.Web.UI.Control
Dim str as String = "wssList"
ctl = New ?????????
Me.Forms.Controls.Add(ctl)
End Sub
Re: [2005] .NET equivalent of COM's CreateObject
Take a look at Activator.CreateInstance method.... Frankly speaking, I've never had the need to use it, so I can't guide you any further.
Re: [2005] .NET equivalent of COM's CreateObject
I've only ever used .CreateInstance with Office. I would think if you have a COM web controls you would instanciate them using the New keyword as long as you have a reference set to the dll.
Re: [2005] .NET equivalent of COM's CreateObject
I found a couple ways of creating the instance and there are probably more. I am not sure which is the better code to use or if it even makes a difference. I will probably use the ObjectHandle method as it would allow me to package the controls in a different Assembly...
Thanks for the help. Here is the code if anyone else has the same question...
Code:
Dim handle As System.Runtime.Remoting.ObjectHandle
Dim ctl As System.Web.UI.WebControls.WebControl
handle = System.Activator.CreateInstance(Nothing, "wssList")
ctl = handle.Unwrap
Controls.Add(ctl)
Dim oType As System.Type
Dim ctl As System.Web.UI.WebControls.WebControl
oType = Type.GetType("wssList", True, True)
ctl = System.Activator.CreateInstance(oType)
Controls.Add(ctl)