使用嵌套名称空间的类库的问题

发布于 2024-10-10 04:34:30 字数 950 浏览 4 评论 0原文

编辑: 我有一个正在快速增长的类库,我决定稍微组织一下...... 类库命名空间的结构如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Web;

namespace ClassLibrary1
{
    namespace FirstNS
    {
        public class Class1
        {
            ReportDocument report = new ReportDocument();

            public static void ShowMe()
            {
                Console.WriteLine("Class1");
            }
        }
    }
    namespace SecondNS
    {
        public class Class2
        {
            public static void ShowMe()
            {
                Console.WriteLine("Class2");
            }
        }
    }
}

它编译得很好。当我尝试通过在控制台应用程序中引用编译的 dll 来使用它时,我可以使用 using 指令引用命名空间。然而,当我尝试构建应用程序时,它说“找不到类型或命名空间名称‘ClassLibrary1’...”。

我发现取出 CrystalDecisions 组件可以解决问题,我只是不确定为什么。 - 尽管听起来很奇怪,但问题与嵌套名称空间有关。如果我使用单独的名称空间,问题就消失了。

EDIT:
I have a class library that is growing rapidly and I've decided to organize it a little...
The class library namespaces are structured like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Web;

namespace ClassLibrary1
{
    namespace FirstNS
    {
        public class Class1
        {
            ReportDocument report = new ReportDocument();

            public static void ShowMe()
            {
                Console.WriteLine("Class1");
            }
        }
    }
    namespace SecondNS
    {
        public class Class2
        {
            public static void ShowMe()
            {
                Console.WriteLine("Class2");
            }
        }
    }
}

It compiles fine. When I try to consume the compiled dll by referencing it in a console application I can reference the namespace with the using directive. However when I try to build the application it says "the type or namespace name 'ClassLibrary1' could not be found...".

I've figured out that taking out the CrystalDecisions assemblies fixes the problem I am just unsure why. - As weird as it sounds somehow the problem relates to having nested namespaces. If I use separate namespaces the problem's gone.

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

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

发布评论

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

评论(2

毅然前行 2024-10-17 04:34:30

好的,根据您的示例和错误,听起来引用您的库的主应用程序无法访问晶体组件。

在您的主应用程序中,添加对 crystal 的引用,或者至少将这些程序集放入您的 bin 目录中。由于主应用程序无法找到装配所需的所有零件,因此无法使用它。

Okay, based on your example and error it sounds like the main application which references your library doesn't have access to the crystal assemblies.

In your main app, add references to crystal OR at the very least put those assemblies in your bin directory. Because the main app can't locate all of the necessary parts for your assembly, it can't use it.

动次打次papapa 2024-10-17 04:34:30

克里斯的回答让我思考了可访问的程序集,我意识到问题是什么。

Crystal 程序集是 Framework 2,主要应用程序是 Framework 4。将目标框架更改回 2 解决了问题。同样,我认为使用 Framework 4 Crystal 组件(如果存在)也应该可以解决问题。

PS:我认为错误消息可能更有帮助......也许指出资源无法访问,而不是整个类库。 :)

Chris's response made me think about accessible assemblies and I realised what the problem was.

The Crystal assemblies are Framework 2 and the main application was Framework 4. Changing the target framework back to 2 fixed the problem. Equally I assume using Framework 4 Crystal assemblies (if they exist) should solve the problem, too.

P.S.: I think the error message could be more helpful though... maybe pointing out that a resource is inaccessible rather than the entire class library. :)

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