0

I'm working on a C# class library project. This project references other DLL files, such as log4net.dll. In this project I have only one class that uses log4net.dll. When other project references my project, I want to copy log4net.dll to its bin folder only if the other project is calling the class that uses log4net.dll.

Is this feasible?

Lucas
  • 884
  • 9
  • 20
  • 1
    How about loading the DLL dynamically and avoiding the hard reference altogether: http://stackoverflow.com/questions/465488/can-i-load-a-net-assembly-at-runtime-and-instantiate-a-type-knowing-only-the-na – DonBoitnott Jun 20 '13 at 14:40
  • That is what I'm doing. I have an adapter that the user can call and set the DLL path. But I don't want the user to do it if I'm able to copy the DLL if I see that the user's project will need it. – Lucas Jun 20 '13 at 14:55

1 Answers1

0

Split your library in two and remove the reference for log4net.dll from the main library, and reference the second library only if you'll need stuff tied to part that need log4net.dll.

Good way how to handle such cases is dependency injection - take a look at Enterprise Library (unity container) - though that will give you one extra dll :)

Using Unity Container:

You'll have Library1 with ILog4NetUsingInterface

In Library2 you'll have class Log4NetUsingClass : ILog4NetUsingInterface

In Library2 you'll have bootstrapper that will register Log4NetUsingClass as implementation of ILog4NetUsingInterface

public static class Bootstrapper {
    public static void Init() {
      ServiceLocator.IoC.RegisterSingleton<ILog4NetUsingInterface, Log4NetUsingClass>();
    }
}

This Init method you'll call only if you need to use the Log4NetUsingClass

In every other library you can just call

ServiceLocator.IoC.Retrieve<ILog4NetUsingInterface>()

(If you won't call bootstrapper it will give you runtime error.)

Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89