我正在做一个C#类库项目。此项目引用其他DLL文件,如log4net.dll。在这个项目中,我只有一个使用log4net.dll的类。当其他项目引用我的项目时,只有当其他项目调用使用log4net.dll的类时,我才希望将log4net.dll复制到它的bin文件夹中。
这可行吗?
发布于 2013-06-20 22:48:43
将你的库一分为二,并从主库中删除对log4net.dll的引用,只有当你需要绑定到需要log4net.dll的部分的东西时才引用第二个库。
处理这种情况的好方法是依赖注入--看看Enterprise Library (unity容器)--尽管这会给你一个额外的dll :)
使用Unity Container:
您将拥有带有ILog4NetUsingInterface的Library1
在Library2中,您将拥有类Log4NetUsingClass : ILog4NetUsingInterface
在Library2中,您将拥有将Log4NetUsingClass注册为ILog4NetUsingInterface实现的引导程序
public static class Bootstrapper {
public static void Init() {
ServiceLocator.IoC.RegisterSingleton<ILog4NetUsingInterface, Log4NetUsingClass>();
}
}只有在需要使用Log4NetUsingClass时才会调用此Init方法
在所有其他库中,您都可以直接调用
ServiceLocator.IoC.Retrieve<ILog4NetUsingInterface>()(如果你不调用bootstrapper,它会给你运行时错误。)
https://stackoverflow.com/questions/17216468
复制相似问题