Adding New References at Runtime...

  • Thread starter Thread starter Juande
  • Start date Start date
J

Juande

Hello,

My application needs load some dll's at runtime, Is there any way to do it?,
can you show me a working example code?

Many thanks
 
Juande said:
My application needs load some dll's at runtime, Is there any way to do
it?

Take a look at the documentation for 'System.Reflection.Assembly.Load*'.
 
Ok thanks,

Now other question please, I have the next code;

Dim Form1 As Form
Form1 =
System.Reflection.Assembly.LoadFrom("MyDLL.dll").CreateInstance("MyDLL.Form2")
Form1.Show()

How can I check if any dll is still loaded?

Many thanks
 
Juande said:
Dim Form1 As Form
Form1 =
System.Reflection.Assembly.LoadFrom("MyDLL.dll").CreateInstance("MyDLL.Form2")
Form1.Show()

How can I check if any dll is still loaded?

The DLL will remain loaded until the appdomain that loaded the DLL and/or
obtained type information from the DLL will be released. If you want to
unload a DLL, you'll have to work with a secondary appdomain and remote
interfaces:

<URL:http://www.west-wind.com/presentations/DynamicCode/DynamicCode.htm>
-> "Understanding how .Net loads code"
 
Juande,

I am curious, how were you able to produce this question in such a short
time, did you really investigate this as Herfried sugested?

Cor
 
Ok,

I have a large C/S Application (sometimes crashes/quits unexpectedly without
any error), I want to divide in modules (dll) and then load the needed ones,
so I've realized a simply application test to load an example dll to check
System.Reflection.Assembly to see how it works, the next step will be check
if the dll is still loaded to doesn't load again.

Herfried; Many thanks for your very quick and useful help.
 
Sorry, but I've got another problem, as you know, I load at runtime my dll,
this dll contains a class called MyForm, example code on a click button
event;

Dim MyDLL As [Assembly]
MyDLL = System.Reflection.Assembly.LoadFrom("MyDLL.dll")
Dim MyForm As Form
MyForm = MyDLL.CreateInstance("MyDLL.MyForm")
MyForm.Show()

This code works fine, but, I want to prevent that if the form is loaded,
doesn't load other instance, How can I do it?

Many thanks in advance
 
Juande said:
Sorry, but I've got another problem, as you know, I load at runtime my
dll, this dll contains a class called MyForm, example code on a click
button event;

Dim MyDLL As [Assembly]
MyDLL = System.Reflection.Assembly.LoadFrom("MyDLL.dll")
Dim MyForm As Form
MyForm = MyDLL.CreateInstance("MyDLL.MyForm")
MyForm.Show()

This code works fine, but, I want to prevent that if the form is loaded,
doesn't load other instance, How can I do it?

\\\
Private m_MyForm As Form
..
..
..
If MyForm Is Nothing Then
MyForm = MyDll.CreateInstance(...)
Else
MsgBox("Form already instantiated!")
End If
///
 
Herfried thanks for your great patience,

When close MyForm and then I want to load it again, it's already
instantiated and doesn't show, in some place of my code when from is closing
I must to set MyForm = nothing, but where?

Thanks

Herfried K. Wagner said:
Juande said:
Sorry, but I've got another problem, as you know, I load at runtime my
dll, this dll contains a class called MyForm, example code on a click
button event;

Dim MyDLL As [Assembly]
MyDLL = System.Reflection.Assembly.LoadFrom("MyDLL.dll")
Dim MyForm As Form
MyForm = MyDLL.CreateInstance("MyDLL.MyForm")
MyForm.Show()

This code works fine, but, I want to prevent that if the form is loaded,
doesn't load other instance, How can I do it?

\\\
Private m_MyForm As Form
.
.
.
If MyForm Is Nothing Then
MyForm = MyDll.CreateInstance(...)
Else
MsgBox("Form already instantiated!")
End If
///
 
Juande said:
When close MyForm and then I want to load it again, it's already
instantiated and doesn't show, in some place of my code when from is
closing I must to set MyForm = nothing, but where?

You may want to add a handler to the form's 'Closed' event using
'AddHandler' and set the variable to 'Nothing' there.
 
Hello Herfried,

Finally I got to dispose the instance of MyForm with AddHandler and
RemoveHandler, thanks to you.
But, every time that I do something about this, problems come to me. The
next question is; How can I access to the methods, functions or variables
declared into 'MyForm'.

One more time, many thanks
 
Juande said:
Finally I got to dispose the instance of MyForm with AddHandler and
RemoveHandler, thanks to you.
But, every time that I do something about this, problems come to me. The
next question is; How can I access to the methods, functions or variables
declared into 'MyForm'.

Direct access to the members would require type information to be available
at compile-time, for example, an interface which defines the methods the
class implements or a design-time reference. Otherwise you'll have to use
reflection (namespace 'System.Reflection') or 'CallByName' to call methods
on the objects.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top