What happens if class is not available?

  • Thread starter Thread starter Diego Armando Maradona
  • Start date Start date
D

Diego Armando Maradona

I have "MyProject.Commons.frmMessageBox"

if I wanna create it, doing this,

using (MyProject.Commons.frmMessageBox xFrm = new
MyProject.Commons.frmMessageBox())
{
xFrm.ShowDialog();
}

if I do not know "MyProject.Commons.frmMessageBox" is available or not and
want to handle this situation like this

if (Available("MyProject.Commons.frmMessageBox"))
{
ShowCustomDialog("MyProject.Commons.frmMessageBox");
}

This is possible ?
 
Why would it not be available? If it not referenced by your project, you will
get a compiler error and visual studio, you will get a compiler warning.
 
Yes, you could probably do something funky with reflection and exception
handling to do this. However I agree with the above poster, if you're trying
to do this you've probably made an incorrect design decision somewhere else.
 
Ciaran said:
Why would it not be available? If it not referenced by your project, you will
get a compiler error and visual studio, you will get a compiler warning.

An assembly containing the referenced class could be replaced later (at
runtime) - just guessing: maybe .NET will display an 'Open File' Dialog
in this case and friendly ask the user to locate a file which contains
the class "MyProject.Commons.frmMessageBox" ...

Error messages are so medieval ;-)

Best Regards
 
The runtime will through an error if the assembly containing the type has
been replaced with a version that doesnt contain it. There is sadly no nice
OpenFileDialog.
 
Diego said:
I have "MyProject.Commons.frmMessageBox"

if I wanna create it, doing this,

using (MyProject.Commons.frmMessageBox xFrm = new
MyProject.Commons.frmMessageBox())
{
xFrm.ShowDialog();
}

if I do not know "MyProject.Commons.frmMessageBox" is available or not
and want to handle this situation like this

if (Available("MyProject.Commons.frmMessageBox"))
{
ShowCustomDialog("MyProject.Commons.frmMessageBox");
}

This is possible ?
I think what you want in this case is a plugin architecture with a
default fallback that just shows a normal messagebox.
You would have to load the assembly dynamically if it exists.
Google c# plugin and you should see how you could do it.

JB
 

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