469,599 Members | 2,666 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,599 developers. It's quick & easy.

Dynamically loading an assembly

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

Nov 17 '05 #1
3 2017
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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ma*******@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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

Nov 17 '05 #2
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.

--
Ming Chen
http://engdump.blogspot.com/
<ma*******@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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

Nov 17 '05 #3
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

ma*******@hotmail.com wrote:
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

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by M. Fitzgerald | last post: by
2 posts views Thread by =?Utf-8?B?SmF0aW5kZXI=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.