473,794 Members | 2,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2165
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.co m

<ma*******@hotm ail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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*******@hotm ail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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 PreloadAssembli es() method
that I call during Main().
private static bool PreloadAssembli es()
{
Assembly assembly = System.Reflecti on.Assembly.Get ExecutingAssemb ly();

AssemblyName[] assemblies = assembly.GetRef erencedAssembli es();

foreach (AssemblyName assemblyName in assemblies)
{
try
{
Assembly.Load(a ssemblyName);
}
catch (System.IO.File NotFoundExcepti on)
{
MessageBox.Show (
"Assembly " + assemblyName.Na me + " was not found.",
"File Not Found",
MessageBoxButto ns.OK,
MessageBoxIcon. Error);
return false;
}
catch (System.BadImag eFormatExceptio n)
{
MessageBox.Show (
"Assembly " + assemblyName.Na me + " is an invalid file
image.",
"Bad Image Format",
MessageBoxButto ns.OK,
MessageBoxIcon. Error);
return false;
}
catch (System.Securit y.SecurityExcep tion)
{
MessageBox.Show (
"You do not have rights to assembly " +
assemblyName.Na me + ".",
"Security Error",
MessageBoxButto ns.OK,
MessageBoxIcon. Error);
return false;
}
catch (System.Excepti on)
{
MessageBox.Show (
"Assembly " + assemblyName.Na me + " cannot be loaded.",
"Assembly Load Error",
MessageBoxButto ns.OK,
MessageBoxIcon. Error);
return false;
}
}
return true;
}

Jason Newell

ma*******@hotma il.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
11688
by: David Elliott | last post by:
I am trying to load an assembly and execute a method from a class. I have listed 3 parts to the code. 1) The Driver to load and execute 2) The Interface 3) The assembly I have a valid referece to the assembly. When I try to create an instance of the class, it returns a null value. Any help would be appreciated.
2
328
by: KK | last post by:
Hi all Can anybody help me what's going wrong here and how to correct it. Code is as follows.... System::Object * GetObjectFromAssembly() { Assembly * pMyAssembly = Assembly::Load("MyAssembly"); Type * pMyType = pMyAssembly->GetType("MyNameSpace.MyClass"); System::Object * pRet = pMyType->CreateInstance(); pMyAssembly = 0;
1
4009
by: Vivek | last post by:
HI Everyone, I am trying to dynamically load a custom deployed assembly from the GAC of a remote machine so that i could call its method from my code using reflection. Can anyone tell me how do i do this? Any help would be appreciated. Thanks Vivek
1
5022
by: Benjamin | last post by:
Hi, I'm currently writing a Web Services that interacts with a database. To allow me to use not just one database provider (for example, I could use MS Access, SQL Server or MySQL), the Web Service dynamically loads up an assembly that implements an Interface I called IDatabase. To load the assembly and create an object, I wrote this function: private IDatabase LoadDatabasePlugin( string assemblyFile, string
1
1276
by: M. Fitzgerald | last post by:
Our migration from ASP to ASP.NET will be a slow one. In themeantime, I have constructed a .NET assembly and registered itas a COM object for use in out classic ASP pages. One of thethings this particular assembly does is use Reflection todynamically load another assembly (based of course onuser-provided parameters). The original error is in that it could not locate the assembly tobe loaded. I've tried placing the dynamically loaded...
6
6879
by: Dan Dorey | last post by:
I actually have two questions here, but I'll start by giving an outline of what I'm trying to do. I'm building an app with a simple plugin architecture (all in the same app domain). I have each plugin in a separate sub-dir inside a "Plugins" directory. Each plugin consists of at least two DLLs (a controller and a driver). However, most drivers also rely on other external DLLs. In one particular case this dependency is to a C++ DLL...
1
1844
by: Bob Rock | last post by:
Hello, I'm new to ASP.NET and I've been looking into the topic of dynamically loading (typically accomplished with a LoadControl followed by a MyControl.Controls.Add()) both user controls and asp.net web controls. I searched for articles on the internet and in the end I must say that I'm confused. I read about issues related to: a.. events not firing
2
3120
by: Smithers | last post by:
Using 3.5, I am stuck in attempting to: 1. Dynamically load an assembly 2. Instantiate a class from that assembly (the client code is in a different namespace than the namespace of the dynamically loaded assembly) so far so good (per my code below)... but here is where I'm getting hung up: 3. Call methods of that type (see comments in my code) If the types in the dynamically loaded assembly were in the same namespace
7
10086
by: chage | last post by:
Hi, I have been searching around to try adding reference assembly to another assembly during runtime, programatically. Is this possible in .Net? The reason for this is because i am having trouble using a library that creates an instance of a Type that i specified, and it failed the locate the Type during runtime, if i do not reference it during compile time.
2
1714
by: =?Utf-8?B?SmF0aW5kZXI=?= | last post by:
Hi, I have written dlls in c# that are being used by other applications. My application works fine if I add these dlls to executable folder,but when I place these dlls in another folder my application crashes. Is there any way to load these dlls from folder other than executable folder. If yes, How will I do that.
0
9671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10433
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.