Adding references during runtime

  • Thread starter Thread starter filip stas
  • Start date Start date
filip stas said:
How do i add references during runtime?

Do you mean references to other assemblies? You don't. You have to load
them with reflection. What *exactly* do you want to do?
 
reference to other assemblies
Jon Skeet said:
How do i add references during runtime?

Do you mean references to other assemblies? You don't. You have to load
them with reflection. What *exactly* do you want to do?
 
filip stas said:
reference to other assemblies

That's not "exactly what you want to do" - that's 4 words. Say exactly
what situation you've got, what the *aim* of this is, etc.
 
[If you could make your newsreader wrap lines at about 70 characters it
would make your posts easier to reply to, by the way.]

filip stas said:
creating dll files containing forms so they can be applied as sort of
an add-in in to a main program. I want to call those dll files and then
dynamically add menus for the forms contained in the dll into the main
program

In that case use Assembly.Load, Assembly.LoadFrom or one of the
similarly named methods. They vary in subtle ways, so be careful which
you pick.

You then use Assembly.GetType(String) to get the appropriate type, then
instantiate it by reflection etc.
 
Okay got at lot further for now, but have a following question

I have a main program wihich merges a menu created in a seperate dll. This mainprogram is an MDIcontainer. How can I make it so that the forms in thd dll use the form of the main program as mdiparent.
these are the codes i have:

testmodule.dll (seperate dll):
class menu
Private _hoofdIn As New MenuItem

Public Function getmenu()

Dim m As New MainMenu

_hoofdIn.Text = "TestModule"

_hoofdIn.MenuItems.Add("subtest", New EventHandler(AddressOf _sub1_Click))

m.MenuItems.Add(_hoofdIn)

Return m

End Function

Protected Sub _sub1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

Dim sf As New sub1

sf.Show()

End Sub

testmodule.dll also contains sub1 form.

The main program:

Imports System.reflection

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'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 MainMenu1 As System.Windows.Forms.MainMenu

Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem

Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem

Friend WithEvents Button1 As System.Windows.Forms.Button

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.MainMenu1 = New System.Windows.Forms.MainMenu

Me.MenuItem1 = New System.Windows.Forms.MenuItem

Me.MenuItem2 = New System.Windows.Forms.MenuItem

Me.Button1 = New System.Windows.Forms.Button

Me.SuspendLayout()

'

'MainMenu1

'

Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})

'

'MenuItem1

'

Me.MenuItem1.Index = 0

Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2})

Me.MenuItem1.Text = "bestand"

'

'MenuItem2

'

Me.MenuItem2.Index = 0

Me.MenuItem2.Text = "sluiten"

'

'Button1

'

Me.Button1.Location = New System.Drawing.Point(104, 104)

Me.Button1.Name = "Button1"

Me.Button1.TabIndex = 0

Me.Button1.Text = "merge"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 273)

Me.Controls.Add(Me.Button1)

Me.IsMdiContainer = True

Me.Menu = Me.MainMenu1

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click

End

End Sub

Private path As String = "c:\testmod"

Private objmenu As MainMenu



Public Sub CreateMenu()

Dim refmodule As [Assembly] = [Assembly].LoadFile(path & "\testmodule.dll")

Dim t As Type = refmodule.GetType("testmodule.menu")

Dim obj As Object = Activator.CreateInstance(t)

Dim ar As Menu = obj.getmenu()

Me.MainMenu1.MergeMenu(ar)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

CreateMenu()

End Sub

End Class

[If you could make your newsreader wrap lines at about 70 characters it
would make your posts easier to reply to, by the way.]

filip stas said:
creating dll files containing forms so they can be applied as sort of
an add-in in to a main program. I want to call those dll files and then
dynamically add menus for the forms contained in the dll into the main
program

In that case use Assembly.Load, Assembly.LoadFrom or one of the
similarly named methods. They vary in subtle ways, so be careful which
you pick.

You then use Assembly.GetType(String) to get the appropriate type, then
instantiate it by reflection etc.
 
filip stas said:
Okay got at lot further for now, but have a following question

<snip>

I'm afraid the combination of:

o Your post's text still not wrapping pleasantly
o The program code having a blank line between each real line
o It being in VB.NET when I'm really a C# coder
o Me not quite understanding what you're after
o Me not knowing a lot about MDI containers
o It being nearly time to go home
o Me not having got much sleep last night

makes me reluctant to tackle this at the moment.

A quick look at it suggests you should:

a) Get familiar with reflection if you're not already
b) Alter the code so it loads the DLL, finds the type, instantiates it,
casts the instance to MenuItem, and then uses it as it would do any
other menu item.

That may well not be what you're after though...
 
Okay got at lot further for now, but have a following question

I have a main program wihich merges a menu created in a seperate dll. This mainprogram is an MDIcontainer. How can I make it so that the forms in thd dll use the form of the main program as mdiparent.
these are the codes i have:

testmodule.dll (seperate dll):
class menu
Private _hoofdIn As New MenuItem

Public Function getmenu()

Dim m As New MainMenu

_hoofdIn.Text = "TestModule"

_hoofdIn.MenuItems.Add("subtest", New EventHandler(AddressOf _sub1_Click))

m.MenuItems.Add(_hoofdIn)

Return m

End Function

Protected Sub _sub1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

Dim sf As New sub1

sf.Show()

End Sub

testmodule.dll also contains sub1 form.

The main program:

Imports System.reflection

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'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 MainMenu1 As System.Windows.Forms.MainMenu

Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem

Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem

Friend WithEvents Button1 As System.Windows.Forms.Button

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.MainMenu1 = New System.Windows.Forms.MainMenu

Me.MenuItem1 = New System.Windows.Forms.MenuItem

Me.MenuItem2 = New System.Windows.Forms.MenuItem

Me.Button1 = New System.Windows.Forms.Button

Me.SuspendLayout()

'

'MainMenu1

'

Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})

'

'MenuItem1

'

Me.MenuItem1.Index = 0

Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2})

Me.MenuItem1.Text = "bestand"

'

'MenuItem2

'

Me.MenuItem2.Index = 0

Me.MenuItem2.Text = "sluiten"

'

'Button1

'

Me.Button1.Location = New System.Drawing.Point(104, 104)

Me.Button1.Name = "Button1"

Me.Button1.TabIndex = 0

Me.Button1.Text = "merge"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 273)

Me.Controls.Add(Me.Button1)

Me.IsMdiContainer = True

Me.Menu = Me.MainMenu1

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click

End

End Sub

Private path As String = "c:\testmod"

Private objmenu As MainMenu



Public Sub CreateMenu()

Dim refmodule As [Assembly] = [Assembly].LoadFile(path & "\testmodule.dll")

Dim t As Type = refmodule.GetType("testmodule.menu")

Dim obj As Object = Activator.CreateInstance(t)

Dim ar As Menu = obj.getmenu()

Me.MainMenu1.MergeMenu(ar)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

CreateMenu()

End Sub

End Class

[If you could make your newsreader wrap lines at about 70 characters it
would make your posts easier to reply to, by the way.]

filip stas said:
creating dll files containing forms so they can be applied as sort of
an add-in in to a main program. I want to call those dll files and then
dynamically add menus for the forms contained in the dll into the main
program

In that case use Assembly.Load, Assembly.LoadFrom or one of the
similarly named methods. They vary in subtle ways, so be careful which
you pick.

You then use Assembly.GetType(String) to get the appropriate type, then
instantiate it by reflection etc.
 
Hi,

I am planning to do a quite similar thing for my
application.
I have different function that belongs to different
assembly. Based on available function on the system I need
my application to be self configurable.
Kind of plug in as you mentionned with menu belonging to
the plugin and forms if any.

Did you finnaly make it happen ? if yes how
do u have sample?

For my case I have only a main form, not MDI

thanks for your info
(I am VB programmer)
regards
Serge
-----Original Message-----
creating dll files containing forms so they can be
applied as sort of an add-in in to a main program. I want
to call those dll files and then dynamically add menus for
the forms contained in the dll into the main program
 
Back
Top