Home All Groups Group Topic Archive Search About

How do you start an assembly dynamically with constructors

Author
1 Dec 2006 8:30 PM
rjjaw
Here is what I have but it isn't working if someone could point me in
the right direction I would greatly appreciate it.

Try
            Dim objAssembly As Reflection.Assembly
            ds.Tables("Main_Menu").DefaultView.RowFilter = "Menu_Name =
'" & sender.text & "'"
            objAssembly =
objAssembly.LoadFrom(ds.Tables("Main_Menu").DefaultView.Item(0).Item("Assembly"))
            Dim typClsX As Type =
objAssembly.GetType(ds.Tables("Main_Menu").DefaultView.Item(0).Item("Assembly_Type"),
True, True)
            objAssembly.GetReferencedAssemblies()
            objAssembly.GetCustomAttributes(True)
            objAssembly.GetEntryAssembly()
            Dim cons() As ConstructorInfo
            cons = typClsX.GetConstructors(BindingFlags.CreateInstance
Or BindingFlags.Instance Or BindingFlags.InvokeMethod Or
BindingFlags.Public Or BindingFlags.NonPublic Or
BindingFlags.DeclaredOnly)
            Dim Params() As ParameterInfo = cons(0).GetParameters()
            Dim MyString() As String = {"Test", "Tester"}
            Dim Frm As New Form
            Frm = Activator.CreateInstance(typClsX.BaseType,
BindingFlags.CreateInstance Or BindingFlags.Instance Or
BindingFlags.Public Or BindingFlags.DeclaredOnly)
            cons.Initialize()



<b>Here is where I recieve the error</b>





Activator.CreateInstance(cons(0).Invoke(BindingFlags.CreateInstance Or
BindingFlags.DeclaredOnly Or BindingFlags.Instance Or
BindingFlags.Public, Nothing, CType(New Object() {New String()
{"TEST"}, New String() {"Tester"}}, Object), Nothing))


        Catch ex As Exception
            MessageBox.Show(ex.ToString & " " & ex.Message & " " &
Err.Source & " " & Err.Description)
        End Try

    End Sub
End Class






<b>This is what I am trying to open.  I am using general code right now
and will apply it to what I need after I figure it out.</b>





ublic Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub SetValues(ByRef label1 As String, ByRef label2 As
String)
        Me.Label2.Text = label1
        Me.Label3.Text = label2
    End Sub

    Friend Sub New(ByVal label1 As String, ByVal label2 As String)
        MyBase.New()
        Me.Label2.Text = Label1
        Me.Label3.Text = Label2

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form
Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(0, 0)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(640, 520)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "This is a test...."
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(16, 32)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(224, 24)
        Me.Label2.TabIndex = 1
        Me.Label2.Text = "Label2"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(16, 88)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(320, 24)
        Me.Label3.TabIndex = 2
        Me.Label3.Text = "Label3"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(640, 518)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Label1.Click

    End Sub
End Class

Author
2 Dec 2006 4:30 AM
Cor Ligthert [MVP]
Rjjaw,

Using reflection is seldom the right route to the good solution. Can you
tell us what you want to achieve, just a bunch of code does not add much to
our own knowledge. And because that this newsgroup is to learn for everybody
from somebody else (How stupid it sometimes is), it needs to have sense.

Cor


Show quoteHide quote
"rjjaw" <joe.wil***@heritagefunds.com> schreef in bericht
news:1165005000.130523.65650@n67g2000cwd.googlegroups.com...
> Here is what I have but it isn't working if someone could point me in
> the right direction I would greatly appreciate it.
>
> Try
>            Dim objAssembly As Reflection.Assembly
>            ds.Tables("Main_Menu").DefaultView.RowFilter = "Menu_Name =
> '" & sender.text & "'"
>            objAssembly =
> objAssembly.LoadFrom(ds.Tables("Main_Menu").DefaultView.Item(0).Item("Assembly"))
>            Dim typClsX As Type =
> objAssembly.GetType(ds.Tables("Main_Menu").DefaultView.Item(0).Item("Assembly_Type"),
> True, True)
>            objAssembly.GetReferencedAssemblies()
>            objAssembly.GetCustomAttributes(True)
>            objAssembly.GetEntryAssembly()
>            Dim cons() As ConstructorInfo
>            cons = typClsX.GetConstructors(BindingFlags.CreateInstance
> Or BindingFlags.Instance Or BindingFlags.InvokeMethod Or
> BindingFlags.Public Or BindingFlags.NonPublic Or
> BindingFlags.DeclaredOnly)
>            Dim Params() As ParameterInfo = cons(0).GetParameters()
>            Dim MyString() As String = {"Test", "Tester"}
>            Dim Frm As New Form
>            Frm = Activator.CreateInstance(typClsX.BaseType,
> BindingFlags.CreateInstance Or BindingFlags.Instance Or
> BindingFlags.Public Or BindingFlags.DeclaredOnly)
>            cons.Initialize()
>
>
>
> <b>Here is where I recieve the error</b>
>
>
>
>
>
> Activator.CreateInstance(cons(0).Invoke(BindingFlags.CreateInstance Or
> BindingFlags.DeclaredOnly Or BindingFlags.Instance Or
> BindingFlags.Public, Nothing, CType(New Object() {New String()
> {"TEST"}, New String() {"Tester"}}, Object), Nothing))
>
>
>        Catch ex As Exception
>            MessageBox.Show(ex.ToString & " " & ex.Message & " " &
> Err.Source & " " & Err.Description)
>        End Try
>
>    End Sub
> End Class
>
>
>
>
>
>
> <b>This is what I am trying to open.  I am using general code right now
> and will apply it to what I need after I figure it out.</b>
>
>
>
>
>
> ublic Class Form1
>    Inherits System.Windows.Forms.Form
>
> #Region " Windows Form Designer generated code "
>
>    Public Sub SetValues(ByRef label1 As String, ByRef label2 As
> String)
>        Me.Label2.Text = label1
>        Me.Label3.Text = label2
>    End Sub
>
>    Friend Sub New(ByVal label1 As String, ByVal label2 As String)
>        MyBase.New()
>        Me.Label2.Text = Label1
>        Me.Label3.Text = Label2
>
>        'This call is required by the Windows Form Designer.
>        InitializeComponent()
>
>        'Add any initialization after the InitializeComponent() call
>
>    End Sub
>
>    'Form overrides dispose to clean up the component list.
>    Protected Overloads Overrides Sub Dispose(ByVal disposing As
> Boolean)
>        If disposing Then
>            If Not (components Is Nothing) Then
>                components.Dispose()
>            End If
>        End If
>        MyBase.Dispose(disposing)
>    End Sub
>
>    'Required by the Windows Form Designer
>    Private components As System.ComponentModel.IContainer
>
>    'NOTE: The following procedure is required by the Windows Form
> Designer
>    'It can be modified using the Windows Form Designer.
>    'Do not modify it using the code editor.
>    Friend WithEvents Label1 As System.Windows.Forms.Label
>    Friend WithEvents Label2 As System.Windows.Forms.Label
>    Friend WithEvents Label3 As System.Windows.Forms.Label
>    <System.Diagnostics.DebuggerStepThrough()> Private Sub
> InitializeComponent()
>        Me.Label1 = New System.Windows.Forms.Label
>        Me.Label2 = New System.Windows.Forms.Label
>        Me.Label3 = New System.Windows.Forms.Label
>        Me.SuspendLayout()
>        '
>        'Label1
>        '
>        Me.Label1.Location = New System.Drawing.Point(0, 0)
>        Me.Label1.Name = "Label1"
>        Me.Label1.Size = New System.Drawing.Size(640, 520)
>        Me.Label1.TabIndex = 0
>        Me.Label1.Text = "This is a test...."
>        '
>        'Label2
>        '
>        Me.Label2.Location = New System.Drawing.Point(16, 32)
>        Me.Label2.Name = "Label2"
>        Me.Label2.Size = New System.Drawing.Size(224, 24)
>        Me.Label2.TabIndex = 1
>        Me.Label2.Text = "Label2"
>        '
>        'Label3
>        '
>        Me.Label3.Location = New System.Drawing.Point(16, 88)
>        Me.Label3.Name = "Label3"
>        Me.Label3.Size = New System.Drawing.Size(320, 24)
>        Me.Label3.TabIndex = 2
>        Me.Label3.Text = "Label3"
>        '
>        'Form1
>        '
>        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
>        Me.ClientSize = New System.Drawing.Size(640, 518)
>        Me.Controls.Add(Me.Label3)
>        Me.Controls.Add(Me.Label2)
>        Me.Controls.Add(Me.Label1)
>        Me.Name = "Form1"
>        Me.Text = "Form1"
>        Me.ResumeLayout(False)
>
>    End Sub
>
> #End Region
>
>    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Label1.Click
>
>    End Sub
> End Class
>
Author
4 Dec 2006 2:08 PM
rjjaw
The application I developed opens slowly because it has to load a lot
of forms that may not be used.  What I am attempting to do, and my
overall goal for the complete project is to only load what I need when
it is request such as the forms.  I would like to make a dashboard that
each of my departments would use and only load their applications they
need at runtime as well.



Cor Ligthert [MVP] wrote:
Show quoteHide quote
> Rjjaw,
>
> Using reflection is seldom the right route to the good solution. Can you
> tell us what you want to achieve, just a bunch of code does not add much to
> our own knowledge. And because that this newsgroup is to learn for everybody
> from somebody else (How stupid it sometimes is), it needs to have sense.
>
> Cor
>
>
> "rjjaw" <joe.wil***@heritagefunds.com> schreef in bericht
> news:1165005000.130523.65650@n67g2000cwd.googlegroups.com...
> > Here is what I have but it isn't working if someone could point me in
> > the right direction I would greatly appreciate it.
> >
> > Try
> >            Dim objAssembly As Reflection.Assembly
> >            ds.Tables("Main_Menu").DefaultView.RowFilter = "Menu_Name =
> > '" & sender.text & "'"
> >            objAssembly =
> > objAssembly.LoadFrom(ds.Tables("Main_Menu").DefaultView.Item(0).Item("Assembly"))
> >            Dim typClsX As Type =
> > objAssembly.GetType(ds.Tables("Main_Menu").DefaultView.Item(0).Item("Assembly_Type"),
> > True, True)
> >            objAssembly.GetReferencedAssemblies()
> >            objAssembly.GetCustomAttributes(True)
> >            objAssembly.GetEntryAssembly()
> >            Dim cons() As ConstructorInfo
> >            cons = typClsX.GetConstructors(BindingFlags.CreateInstance
> > Or BindingFlags.Instance Or BindingFlags.InvokeMethod Or
> > BindingFlags.Public Or BindingFlags.NonPublic Or
> > BindingFlags.DeclaredOnly)
> >            Dim Params() As ParameterInfo = cons(0).GetParameters()
> >            Dim MyString() As String = {"Test", "Tester"}
> >            Dim Frm As New Form
> >            Frm = Activator.CreateInstance(typClsX.BaseType,
> > BindingFlags.CreateInstance Or BindingFlags.Instance Or
> > BindingFlags.Public Or BindingFlags.DeclaredOnly)
> >            cons.Initialize()
> >
> >
> >
> > <b>Here is where I recieve the error</b>
> >
> >
> >
> >
> >
> > Activator.CreateInstance(cons(0).Invoke(BindingFlags.CreateInstance Or
> > BindingFlags.DeclaredOnly Or BindingFlags.Instance Or
> > BindingFlags.Public, Nothing, CType(New Object() {New String()
> > {"TEST"}, New String() {"Tester"}}, Object), Nothing))
> >
> >
> >        Catch ex As Exception
> >            MessageBox.Show(ex.ToString & " " & ex.Message & " " &
> > Err.Source & " " & Err.Description)
> >        End Try
> >
> >    End Sub
> > End Class
> >
> >
> >
> >
> >
> >
> > <b>This is what I am trying to open.  I am using general code right now
> > and will apply it to what I need after I figure it out.</b>
> >
> >
> >
> >
> >
> > ublic Class Form1
> >    Inherits System.Windows.Forms.Form
> >
> > #Region " Windows Form Designer generated code "
> >
> >    Public Sub SetValues(ByRef label1 As String, ByRef label2 As
> > String)
> >        Me.Label2.Text = label1
> >        Me.Label3.Text = label2
> >    End Sub
> >
> >    Friend Sub New(ByVal label1 As String, ByVal label2 As String)
> >        MyBase.New()
> >        Me.Label2.Text = Label1
> >        Me.Label3.Text = Label2
> >
> >        'This call is required by the Windows Form Designer.
> >        InitializeComponent()
> >
> >        'Add any initialization after the InitializeComponent() call
> >
> >    End Sub
> >
> >    'Form overrides dispose to clean up the component list.
> >    Protected Overloads Overrides Sub Dispose(ByVal disposing As
> > Boolean)
> >        If disposing Then
> >            If Not (components Is Nothing) Then
> >                components.Dispose()
> >            End If
> >        End If
> >        MyBase.Dispose(disposing)
> >    End Sub
> >
> >    'Required by the Windows Form Designer
> >    Private components As System.ComponentModel.IContainer
> >
> >    'NOTE: The following procedure is required by the Windows Form
> > Designer
> >    'It can be modified using the Windows Form Designer.
> >    'Do not modify it using the code editor.
> >    Friend WithEvents Label1 As System.Windows.Forms.Label
> >    Friend WithEvents Label2 As System.Windows.Forms.Label
> >    Friend WithEvents Label3 As System.Windows.Forms.Label
> >    <System.Diagnostics.DebuggerStepThrough()> Private Sub
> > InitializeComponent()
> >        Me.Label1 = New System.Windows.Forms.Label
> >        Me.Label2 = New System.Windows.Forms.Label
> >        Me.Label3 = New System.Windows.Forms.Label
> >        Me.SuspendLayout()
> >        '
> >        'Label1
> >        '
> >        Me.Label1.Location = New System.Drawing.Point(0, 0)
> >        Me.Label1.Name = "Label1"
> >        Me.Label1.Size = New System.Drawing.Size(640, 520)
> >        Me.Label1.TabIndex = 0
> >        Me.Label1.Text = "This is a test...."
> >        '
> >        'Label2
> >        '
> >        Me.Label2.Location = New System.Drawing.Point(16, 32)
> >        Me.Label2.Name = "Label2"
> >        Me.Label2.Size = New System.Drawing.Size(224, 24)
> >        Me.Label2.TabIndex = 1
> >        Me.Label2.Text = "Label2"
> >        '
> >        'Label3
> >        '
> >        Me.Label3.Location = New System.Drawing.Point(16, 88)
> >        Me.Label3.Name = "Label3"
> >        Me.Label3.Size = New System.Drawing.Size(320, 24)
> >        Me.Label3.TabIndex = 2
> >        Me.Label3.Text = "Label3"
> >        '
> >        'Form1
> >        '
> >        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
> >        Me.ClientSize = New System.Drawing.Size(640, 518)
> >        Me.Controls.Add(Me.Label3)
> >        Me.Controls.Add(Me.Label2)
> >        Me.Controls.Add(Me.Label1)
> >        Me.Name = "Form1"
> >        Me.Text = "Form1"
> >        Me.ResumeLayout(False)
> >
> >    End Sub
> >
> > #End Region
> >
> >    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As
> > System.EventArgs) Handles Label1.Click
> >
> >    End Sub
> > End Class
> >