两个 VS2008 C# 类库项目是否可以共享同一个命名空间?

发布于 2024-09-04 09:12:36 字数 1023 浏览 2 评论 0原文

我试图在单个解决方案中的两个项目之间共享公共名称空间。这些项目是“蓝图”和“存储库”。蓝图包含整个应用程序的接口,并作为应用程序结构的参考。

在蓝图项目中,我有一个带有以下声明的接口:

namespace Application.Repositories{
    public interface IRepository{
        IEntity Get(Guid id);
    }
}

在存储库项目中,我有一个类以下类:

namespace Application.Repositories{
    public class STDRepository: IRepository
    {
        STD Get(Guid id){
             return new SkankyExGirlfriendDataContext()
                           .FirstOrDefault<STD>(x=>x.DiseaseId == id);
        }
    }
}

但是,这不起作用。 Repositories 项目引用了 Blueprint 项目。我收到 VS 错误:“找不到类型或命名空间名称‘IRepository’(您是否缺少 using 指令或程序集引用?) - 通常,这很容易修复,但添加 using 语句没有意义因为它们具有相同的命名空间。无论如何,我尝试了它,但它没有工作。并且没有引用该接口的代码行,我在这里迷失了一切。没有发现任何东西,所以我假设我正在做的事情有根本性的错误......但我不知道它是什么,所以,我希望得到一些关于如何解决这个问题的解释或指导。我希望你们能帮忙。

注意:我之所以想这样做并将接口保留在同一名称空间下是因为我想要一个可靠的项目来保留所有接口,以便有一个参考。我考虑过解决方法,例如将所有接口放在 Blueprint.Application 命名空间中,而不是应用程序命名空间中。但是,这需要我在应用程序的几乎每个页面上编写 using 语句......而且我的手指会很累。再次感谢大家...

I am trying to share a common namespace between two projects in a single solution. The projects are "Blueprint" and "Repositories". Blueprint contains Interfaces for the entire application and serves as a reference for the application structure.

In the Blueprint project, I have an interface with the following declaration:

namespace Application.Repositories{
    public interface IRepository{
        IEntity Get(Guid id);
    }
}

In the Repositories project I have a class the following class:

namespace Application.Repositories{
    public class STDRepository: IRepository
    {
        STD Get(Guid id){
             return new SkankyExGirlfriendDataContext()
                           .FirstOrDefault<STD>(x=>x.DiseaseId == id);
        }
    }
}

However, this does not work. The Repositories project has a reference to the Blueprint project. I receive a VS error: "The type or namespace name 'IRepository' could not be found (are you missing a using directive or an assembly reference?) - Normally, this is easy to fix but adding a using statement doesn't make sense since they have the same namespace. I tried it anyway and it didn't work. The reference has been added, and without the line of code referencing that interface, both projects compile successfully. I am lost here. I have searched all over and have found nothing, so I am assuming that there is something fundamentally wrong with what I'm doing ... but I don't know what it is. So, I would appreciate some explanation or guidance as to how to fix this problem. I hope you guys can help.

Note: The reason I want to do it this way and keep the interfaces under the same namespace is because I want a solid project to keep all the interfaces in, in order to have a reference for the full architecture of the application. I have considered work arounds, such as putting all of the interfaces in the Blueprint.Application namespace instead of the application namespace. However, that would require me to write the using statement on virtually every page in the application...and my fingers get tired. Thanks again guys...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾北清歌寒 2024-09-11 09:12:36

这是可能的。

例如,mscorlib.dll 和 System.dll 是两个不同的 C# 项目,它们都在 System 命名空间中定义类。 (除其他外)

如果 Repositories 项目未引用 Blueprint,就会发生此错误。

This is possible.

For example, mscorlib.dll and System.dll are two different C# projects that both define classes in the System namespace. (among others)

This error would happen if the Repositories project doesn't reference Blueprint.

_失温 2024-09-11 09:12:36

好吧 - 所以,我将 SLAks 标记为正确,因为他的答案是正确的,并且可能会成为将来看到此问题的任何其他人的解决方案。我的问题与我的解决方案中的其他 6 个项目文件有关。我卸载了它们并仅使用两个已发布的实例重建了解决方案,突然我的智能感知开始工作。尽管这两个项目没有错误,但其他项目中的错误导致编译出现一些问题。该死...谢谢大家。

Alright - So, I marked SLaks as right because his answer was correct and will probably be the solution for any one else viewing this in the future. My problem had to do with the other 6 project files in my solution. I unloaded them and rebuilt the solution with only the two posted instances and suddenly my Intellisense started working. Even though these two projects had no errors, the errors in other projects were causing some problem in the compile. Damn... Thanks guys.

感情旳空白 2024-09-11 09:12:36

对于您想要归档的内容,您还可以尝试将所有内容放入一个程序集中,并将所有类声明为 internal 而不是 public。这样使用它的程序只会看到接口。您将必须创建一组工厂类,这些工厂类根据某些参数创建特定接口的实例。这确实是正确的方法,因为小组件会导致大量开销。请参阅Microsoft 性能指南

For what you are trying to archieve, you can also try to put everything into one assembly and declare all classes as internal instead of public. This way the program that uses it will only see the interfaces. You will have to create a set of factory classes that create instances of specific interfaces depending on some parameters. This is really the way to go, because the small assemblies cause a lot of overhead. See the Microsoft Performance Guidelines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文