简体   繁体   English

C#项目参考的问题

[英]C# project reference's question

I have ac# solution and its composed of numerous projects. 我有ac#解决方案,它由众多项目组成。

I have a project that is my baseassemblies that holds all common information that other projects use. 我有一个项目是我的基础组件,包含其他项目使用的所有常见信息。 All of the other projects have references to baseassemblies. 所有其他项目都引用了基础组件。

I added a dll reference in my baseassemblies however all of the other projects cant see it. 我在我的baseassemblies中添加了一个dll引用,但所有其他项目都看不到它。

How can I make it so that the other projects can see the DLL that baseassemblies is referencing? 我怎样才能使其他项目可以看到baseassemblies正在引用的DLL? I dont want to have to add the DLL to all of the projects since that defeats the purpose of my baseassemblies project. 我不想将DLL添加到所有项目中,因为这会破坏我的基础组件项目的目的。

The correct approach is for your other assemblies NOT to need a reference to that other DLL. 正确的方法是让您的其他程序集不需要引用其他DLL。 The way to correctly do that, is to not have your base assemblies expose any of the types that are within that DLL. 正确执行此操作的方法是不让基本程序集公开该DLL中的任何类型。 Wrap all the functionality that you need in the base assemblies, and make sure that whoever consumes your base assemblies, needs NO knowledge of the underlying dll's base assemblies is using. 在基础程序集中包含所需的所有功能,并确保使用基础程序集的任何人都不需要了解底层dll的基本程序集。 Otherwise, each project that will reference your base assemblies, if they need to use something that's contained in that dll, they'll have to reference it. 否则,每个将引用基本程序集的项目,如果需要使用该dll中包含的内容,则必须引用它。

There are no transitive references in .NET. .NET中没有传递引用。 If an assembly needs to reference another it must do so directly, it cannot "inherit" that reference from another reference. 如果程序集需要引用另一个程序集,则它必须直接执行,它不能“继承”来自另一个引用的引用。

Note, a project only needs to reference assemblies it directly uses types from. 注意,项目只需要引用它直接使用类型的程序集。 If A uses B, and B uses C, but A does not directly use C, then A only needs to reference B directly (the loader will handle B referencing C). 如果A使用B,而B使用C,但A不直接使用C,那么A只需要直接引用B(加载器将处理B引用C)。

In short you can't do it. 总之,你不能这样做。 You need to add a reference to the DLL containing the code you are trying to use otherwise it won't be able to see it. 您需要添加对包含您尝试使用的代码的DLL的引用,否则将无法看到它。

My suggestion would be to create a layer in your 'BaseAssemblies' project which you can access from your application which essentially creates a tiered architecture. 我的建议是在你的'BaseAssemblies'项目中创建一个层,你可以从你的应用程序访问它,从而创建一个分层架构。

example

Application Layer - Uses IDataClass 应用层 - 使用IDataClass
Business Logic Layer - Defines IDataClass 业务逻辑层 - 定义IDataClass
Data Access Layer - MyRawDataClass (implements IDataClass) 数据访问层 - MyRawDataClass(实现IDataClass)

From the example, the application layer only needs a reference to the BAL to be able to interact with the DAL. 从示例中,应用程序层仅需要对BAL的引用才能与DAL交互。

There are instances where your project refers A which in turn refers B but when you build, B isn't always there in the BIN folder and you only realize this when the code is running. 在某些情况下,您的项目会引用A,而A又会引用B,但在构建时,B并不总是存在于BIN文件夹中,并且您只在代码运行时才意识到这一点。 There is a related SO Question HERE . 有一个SO提问相关这里 People have solved it in bizarre ways, but I specifically liked the solution by John Hunter. 人们以奇怪的方式解决了这个问题,但我特别喜欢John Hunter的解决方案。 This SO thread also discusses a solution where you use build events to achieve the desired results. SO线程还讨论了使用构建事件来实现所需结果的解决方案。

You can have your BaseAssemblies export Interfaces that have to be implemented by your "other" dll. 您可以让BaseAssemblies导出必须由“其他”dll实现的接口。 Additionally, your BaseAssemblies need some "class factory" functionality for those classes. 此外,您的BaseAssemblies需要为这些类提供一些“类工厂”功能。

Other posters are correct: you can't do it. 其他海报是正确的:你做不到。 This is, IMHO, pathetic on the part of Visual Studio. 这是,恕我直言,可怜的Visual Studio。 "make" handled this clean as pie 20 years ago... 20年前,“制造”处理这个干净的馅饼......

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

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