【问题标题】:Namespace and base class not recognized - Why?名称空间和基类无法识别 - 为什么?
【发布时间】:2020-05-09 16:40:23
【问题描述】:

所以我正在做一个别人给我的项目。

在项目中,有一个主要的Namespace,我们称之为“Program”。 在该命名空间中有几个不同的类。

现在我必须从命名空间Devices 中的一个类派生。 已经有另一个类派生自对象Device。 所以我继续,创建了我的类,将它包含在 Devices 命名空间中,从 Device 派生它并完成了我的工作。

但不久之后,我无法使用任何基本变量、方法等。整个智能感知不适用于该课程。 更糟糕的是,它似乎没有包含在程序集中。

如何让 Intellisense、程序集等识别我的课程?我做了一个测试类,只是这样:

Namespace Devices
    Class Testcle
        ' ... Nothing
    End Class
End Namespace 

而且效果很好。它被包含在程序集中,Intellisense 工作,等等。

文件夹结构有点像这样:

[Project]
↳ [-] Devices (Folder and Namespace)
    [-] AlreadyExistingClassDerivedFromDevice (Folder)
        ↳ AlreadyExistingClassDerivedFromDevice.vb
    [-] MyClassDerivedFromDevice (Folder)
        ↳ MyClassDerivedFromDevice.vb
↳ [+] Other (Folder)
↳ Testcle.vb
↳ Device.vb

我有什么遗漏吗?比如有没有我必须激活的隐藏设置?


编辑: 声明看起来有点像这样(但在不同的文件中):

设备.vb

Namespace Devices
    Public MustInherit Class Device
    ' ...
    End Class
End Namespace

这行得通:

AlreadyExistingClassDerivedFromDevice.vb

Namespace Devices
   Public NotInheritable Class AlreadyExistingClassDerivedFromDevice
        Inherits Device
   ' ...
   End Class
End Namespace

这不起作用:

MyClassDerivedFromDevice.vb

Namespace Devices
   Public NotInheritable Class MyClassDerivedFromDevice
        Inherits Device
   ' ...
   End Class
End Namespace

继承类实际上没有区别。只有内部运作。但是那些不应该对ctor或Intellisense或其他东西的可访问性产生影响,对吧?

【问题讨论】:

  • 你有Public Class Testcle 还是只有Class Testcle
  • Public NotInheritable Class MyClassDerivedFromDevice Inherits Device
  • 那么,哪一个不工作? MyClassDerivedFromDevice.vb 还是 Testcle.vb?你能发布他们的声明,包括额外的/嵌套的命名空间吗?
  • MyClassDerivedFromDevice.vb 不工作。是的,我会尽快将它们缩小到最低限度。不幸的是,我现在没有时间。
  • 既然您提到了与命名空间有关的文件夹结构(“设备(文件夹和命名空间)”),我怀疑您已经习惯了在将类添加到文件夹; VB 不这样做。您必须使用 NameSpace 块显式包装类。您可以使用“类视图”(View Menu->Other Windows->Class View)来确定类在命名空间树中的位置。使用最小化的代码发布,很难推测问题出在哪里。

标签: vb.net intellisense .net-assembly


【解决方案1】:

我写这个作为答案,因为它解决了我的问题。但是,我不知道它有什么原因,也不知道以“正确”方式实际解决问题的确切步骤。

我所做的如下:

我将文件 MyClassDerivedFromDevice.vb 重命名为 MyClassDerivedFromDevice_Old.vb,创建了一个名为 MyClassDerivedFromDevice.vb 的新类,在添加命名空间并确保 Intellisense 和一切正常之后,我只是从 MyClassDerivedFromDevice_Old.vb 复制了代码(在将Namespace DevicesEnd Namespace 转换为MyClassDerivedFromDevice.vb

这解决了问题。


短版:

1) 将NotWorkingClass.vb 重命名为NotWorkingClass_Old.vb

2) 添加名称为NotWorkingClass.vb 的新类。

3) 将命名空间添加到NotWorkingClass.vb

4) 检查是否一切正常(Intellisense 等)并被 Visual Studio 识别。

5) 将Namespace X 之后的所有代码复制到End Namespace 之前的所有代码,从NotWorkingClass_Old.vb 复制到NotWorkingClass.vb(现在应该可以工作了)。

【讨论】:

  • 看来这只是一个 IntelliSense 故障(经常发生)。我正要问您,当您尝试手动访问基类(不依赖 IntelliSense)时,编译器是否会报错。
  • 是的,也许我应该尝试一下,但是这个程序很大,所以我必须在访问它之前围绕它构建一个结构。我应该将此标记为已接受的答案吗?
猜你喜欢
  • 1970-01-01
  • 2011-02-11
  • 1970-01-01
  • 2014-01-21
  • 1970-01-01
  • 2017-10-24
  • 2011-05-12
  • 1970-01-01
  • 2019-12-19
相关资源
最近更新 更多