简体   繁体   English

C#解决方案:为外部dll使用一个“Globals”项目?

[英]C# solutions : using one “Globals” project for external dll's?

(Sorry for might be a trivial question , I'm coming from Java & Maven , and still haven't wrapped my mind around C# dependencies ) (抱歉,这可能是一个微不足道的问题,我来自Java和Maven,仍然没有把我的思想包裹在C#依赖项中)

I want to use log4net in all my projects. 我想在我的所有项目中使用log4net。 Since I don't want to add the dll to all the projects , I've created a "Globals" project , add a reference to log4net.dll in it , and referenced from all the other projects to the "Globals" project . 由于我不想将dll添加到所有项目中,因此我创建了一个“Globals”项目,在其中添加对log4net.dll的引用,并将所有其他项目引用到“Globals”项目。

However , I can't seem to access the log4net classes from any other project . 但是,我似乎无法从任何其他项目访问log4net类。

using Globals.log4net;

Doesn't seems to work either . 似乎也没有用。

What am I doing wrong? 我究竟做错了什么?

If all you did was reference the DLL, then all you have done was get a copy of the DLL with every reference to your Globals project. 如果您所做的只是引用DLL,那么您所做的就是获取DLL的副本 ,每次引用您的Globals项目。 You are still not using the library. 您仍然没有使用该库。

What I would normally do would create an ILogger interface, implement it using log4net in the Globals project and use that implementation in the other projects (plus a mock implementation for tests). 我通常会创建一个ILogger接口,使用Globals项目中的log4net实现它,并在其他项目中使用该实现(以及测试的模拟实现)。

I'm afraid that's not how it works. 我担心这不是它的工作方式。 You have to add the DLL to all projects you want to call it from. 您必须将DLL添加到要从中调用它的所有项目。

If you were only using a couple of functions in the DLL, you could create functions in your Globals project to call through to it. 如果您只在DLL中使用了几个函数,则可以在Globals项目中创建函数来调用它。

log4net doesn't 'live' in Globals simply by the reference. log4net不仅仅通过引用在Globals中“存在”。

My 1st inclination would be to have all of your projects just reference log4net, it clarifies that there's a dependency there no need to hide it in another project. 我的第一个倾向是让你的所有项目都引用log4net,它澄清了依赖,不需要在另一个项目中隐藏它。

However, if you do have common logic shared across your classes you could have a "Global" or "Common" class which includes references to shared libraries. 但是,如果您在类之间共享共享逻辑,则可以使用“全局”或“公共”类,其中包括对共享库的引用。 To reference those libraries just add the using of the target namespace. 要引用这些库,只需添加目标命名空间的使用。

In other words, no matter if the reference is within the same project or another reference project, the using statement will be the same. 换句话说,无论引用是在同一项目还是另一个引用项目中,using语句都是相同的。

For log4net i believe it should just be: 对于log4net我相信它应该只是:

using log4net;

The other way to add the proper reference would be to type one of the class names somwhere in your code ( Logger ? ) and then invoke the helper menu with "CTRL+." 添加正确引用的另一种方法是在代码中键入其中一个类名( Logger ?),然后使用“CTRL +”调用帮助器菜单。 or by simply expanding it, this will have the option to add the proper using statement. 或者通过简单地扩展它,可以选择添加正确的using语句。

That system won't work. 那个系统不起作用。 You'll have to add the log4net dll as a reference to all the projects. 您必须添加log4net dll作为所有项目的参考。 Or create proxy classes, which is much more work. 或者创建代理类,这是更多的工作。

Read up on the GAC (Global Assembly Cache), this a central storage for DLLs that are shared across projects... thats where I put my log4net DLL. 阅读GAC(全局程序集缓存),这是一个跨项目共享的DLL的中央存储...这就是我放置log4net DLL的地方。 You can then simply add the reference to it in your .config file forevery project you need to use it in without adding the DLL to the projects themselves. 然后,您只需在.config文件forevery项目中添加对它的引用即可,无需将DLL添加到项目本身。

This is a good place to start: MSDN: Working with the Global Assembly Cache 这是一个很好的起点: MSDN:使用全局程序集缓存

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM