Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing an external .NET DLL provided by another application in C#

I have a C# project which references a DLL (call it external DLL) which comes with another application. When I build my project, due to the reference, the external DLL gets automatically added to my project output. And when I run my project it loads the external DLL from my project folder.

The other application, which the external DLL belongs to, is developed by another team and the DLL is regularly updated. I don't want to package their DLL with my project. Instead I would like to have my project load their DLL when executed -- rather than pick the DLL copy from my project's folder.

Now I know that this is possible through reflection. I know that I can do an "Assembly.Load" and pick the DLL. But because I use the types from the external DLL all through my code, I would like the code to be statically type checked.

Here's what I would like:

  1. Be able to compile my project by referencing the external DLL and thus get static type checking.
  2. When the project is run, the external DLL is picked up from the other application's folder and not the copy of the DLL which is in my project's output folder.

Is there any way to solve this problem? Is there some middle ground between adding a reference and using reflection?

like image 836
Rohit Avatar asked Aug 02 '11 23:08

Rohit


People also ask

How do you reference other projects in C#?

Restart Visual Studio, create a new C# UWP app project, and then right-click on the project and choose Add Reference. Go to the Windows tab, then the Extensions sub-tab, and select the Extension SDK. Look at the right pane in the Reference Manager. If it has dependencies, they will be listed there.

Can a DLL reference another DLL?

Any DLL that lays somewhere in file system can be referenced. After compilation this DLL must be either in GAC (Global Assembly Cache) or in same directory with the working application.

How do I add a reference to System Web DLL?

1 right click on References in Solution Explorer and press add reference... 2 choose the browse tab and go to C:\Windows\assembly\GAC_32\System. Web\System. Web.


1 Answers

The most immediete solution to your problem is to change the properties of the reference. There is a setting called Copy Local. Set that to false and it'll stop copying the DLL to your project's output. You can access the properties of the reference by expanding the references folder in your solution, right-clicking on the reference in question, and clicking properties to open the properties pane.

The fact that Visual Studio copies the DLL to your project's output folder at build time doesn't really matter to the .Net Framework at runtime. All that matters is that the assemblies you reference are available to the framework either in the paths it searches or in the global assembly cache.

like image 60
Scott Pedersen Avatar answered Nov 03 '22 18:11

Scott Pedersen