Dynamically loading an assembly

M

markoueis

Our .NET application, let's call it A, needs to load an assembly of
another .NET application, let's call it B. Preferably I would like to
early bind and early load the B assembly. However, in a deployment
scenario, it is possible that B is not installed, in which case, the
dependencies for its assembly won't be there. When I try to load that
assembly (which is copied) regardless, my application crashes saying
that its dependencies cannot be found.

What's the best way to solve this problem? I could late bind the
assembly, but then do I really have to use reflection for each and
every method call I make against this assembly? If this is so, are
there any OO design techniques out there I can use? Is there any way I
can still compile against that assembly?

Are there any good solid articles on this kind of development?

Thanks

Mark
 
N

Nicholas Paldino [.NET/C# MVP]

Mark,

In order to get around this, you will want another assembly which
contains interfaces to the types you are using. Then, you would reference
this assembly in assembly B, and implement the interfaces.

Also, in assembly A, you would reference these interfaces and then call
the methods on them when something in the implementation changes.

This would require you to have a class factory of some sort in order to
create the instances (since you couldn't directly reference the assembly).

I know this is just me, but honestly, I would live with the error, and
address it in the install. This is a hell of a lot of work in order to
produce a "friendly" message for a corrupt install.

Hope this helps.
 
M

Ming Chen

Hi, Mark

You don't have to use reflection for every method call if the class
defined in B implements an interface that is accessible in A.

For example, you can define assembly C, which contains an interface
definition for a type in B. Reference C when you are building A. At runtime,
after creating an object that is defined in B using reflection, you can
immediately cast it to the interface defined in C and then you don't need to
use reflection in subsequent calls.
 
J

Jason Newell

Mark,

This is how I handle it. I have a static PreloadAssemblies() method
that I call during Main().


private static bool PreloadAssemblies()
{
Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();

AssemblyName[] assemblies = assembly.GetReferencedAssemblies();

foreach (AssemblyName assemblyName in assemblies)
{
try
{
Assembly.Load(assemblyName);
}
catch (System.IO.FileNotFoundException)
{
MessageBox.Show(
"Assembly " + assemblyName.Name + " was not found.",
"File Not Found",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return false;
}
catch (System.BadImageFormatException)
{
MessageBox.Show(
"Assembly " + assemblyName.Name + " is an invalid file
image.",
"Bad Image Format",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return false;
}
catch (System.Security.SecurityException)
{
MessageBox.Show(
"You do not have rights to assembly " +
assemblyName.Name + ".",
"Security Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return false;
}
catch (System.Exception)
{
MessageBox.Show(
"Assembly " + assemblyName.Name + " cannot be loaded.",
"Assembly Load Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return false;
}
}
return true;
}

Jason Newell
 

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

Top