简体   繁体   English

为什么我不需要在测试类文件中添加我的主项目的命名空间?

[英]Why don't I need to add my main project's namespace in the test class files?

Hopefully an elementary question, but one to which I can't find an answer...希望是一个基本的问题,但我找不到答案......

This Microsoft documentation leads me to believe that I need to add a using directive for my main project's namespace to my test class files, in order to be able to access the main project's members. 这个 Microsoft 文档让我相信我需要为我的主项目的命名空间添加一个using指令到我的测试类文件中,以便能够访问主项目的成员。

using MainProject;

namespace MainProject.Tests;

[TestClass]
public class UnitTests
{
    ...
}

However, when I do so, Visual Studio Code (with the Microsoft C# extension) tells me the MainProject import is unnecessary.但是,当我这样做时,Visual Studio Code(带有 Microsoft C# 扩展名)告诉我MainProject导入是不必要的。 And indeed, my unit tests manage to access the main project members without the using statement.事实上,我的单​​元测试在没有using语句的情况下设法访问主要项目成员。

Both the main project and the test project exist in separate directories and have separate .csproj files.主项目和测试项目都存在于不同的目录中,并具有单独的.csproj文件。 Both live inside a parent folder containing an .sln file to which each has been added.两者都存在于包含每个已添加到其中的.sln文件的父文件夹中。 The test project's .csproj file has a reference to the main project's.测试项目的.csproj文件引用了主项目的。

The main project's namespace is: MainProject主项目的命名空间是: MainProject

The test project's namespace is: MainProject.Tests测试项目的命名空间是: MainProject.Tests

I have opted into global usings in the test project file, and there is a usings.cs file, which contains one line:我在测试项目文件中选择了全局使用,并且有一个usings.cs文件,其中包含一行:

global using Microsoft.VisualStudio.TestTools.UnitTesting;

Where is the magic happening?魔法发生在哪里? (For example, is there a rule that the "child" namespace automatically "inherits" the types declared in the parent?) (例如,是否存在“子”命名空间自动“继承”父级中声明的类型的规则?)

Many thanks for your help.非常感谢您的帮助。

A usising statement simple brings the namespace in scope for name resolution , it has nothing to do with directories or assemblies.一个简单的使用语句将命名空间带入名称解析的范围内,它与目录或程序集无关。

The namespace does the same thing, except it has a hierarchical basis and so isn't as flexible.命名空间做同样的事情,除了它有一个层次结构,所以不那么灵活。

In essence, there is no magic, you have misunderstood what is happening.本质上,没有魔法,你误解了正在发生的事情。

I found the official documentation explaining that namespace scopes "nest", and that the inner namespace has access to the members of the outer.我发现官方文档解释了命名空间范围“嵌套”,并且内部命名空间可以访问外部命名空间的成员。 Here it is: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/basic-concepts#77-scopes这是: https ://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/basic-concepts#77-scopes

(The second bullet point in section 7.7.1 describes the rule.) (第 7.7.1 节中的第二个要点描述了该规则。)

When I last worked with C#, a long time ago, it was similar enough to Java to be virtually indistinguishable.很久以前,当我上次使用 C# 时,它与 Java 非常相似,几乎无法区分。 But a lot has changed.但是很多东西都变了。

For example, I had forgotten that the Java-style namespace declarations are a recent addition to dotnet:例如,我忘记了 Java 风格的命名空间声明是最近添加到 dotnet 的:

namespace MyCompany.MyProduct;
...

We used to have to do it like this:我们以前必须这样做:

namespace MyCompany
{
    namespace MyProduct
    {
        ...
    }
}

Looking at it this way, it's easy to see why one might implicitly presume that the inner scope has access to the members of the outer, without having to read the rule from the language spec.以这种方式看待它,很容易理解为什么人们可能会隐含地假定内部范围可以访问外部的成员,而无需阅读语言规范中的规则。 If you're used to JavaScript scopes, for example, and your mental model of namespaces looks like the above, then the idea just seems natural.例如,如果您习惯于 JavaScript 作用域,并且您的命名空间心智模型看起来像上面那样,那么这个想法似乎很自然。

However, if you're used to the way package imports work in Java, then this can seem very strange.但是,如果您习惯于 Java 中包导入的工作方式,那么这似乎很奇怪。

暂无
暂无

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

相关问题 为什么当我将此类带入我的项目时找不到某些名称空间\\类? - Why when I take this class into my project don't find some namespaces\classes? 我的项目中没有“添加引用” - I don't have "add reference" in my project 当我没有放置命名空间时,我的类有什么命名空间? - What namespace are my classes in when I don't put a namespace? 我不知道为什么我的班级无法序列化 - I don't know why my class cannot be serialized 为什么我在Visual Studio中的对象浏览器中找不到System.Windows.Controls命名空间? - Why don't I find the System.Windows.Controls namespace in my object browser in Visual Studio? 在引用枚举类型时,为什么需要在枚举的声明中添加名称空间和类? - in referencing enum type why do i need to add namespace and class in declaration of the enum? 为什么我不能在同一解决方案中的另一个项目中引用另一个命名空间中的类? - Why can't I reference a class in another namespace in another project within the same solution? 为什么我在设置本地 XML 命名空间后没有得到我项目中页面的智能感知建议 - Why dont't I get the intellisense suggestions for the pages in my project after setting up the local XML namespace 将类移至其他名称空间,是否可以向文件添加隐式使用? - Moved a class to a different namespace, is it possible to add an implicit using to my files? 获取我的测试班级的命名空间 - Get NameSpace of my Test Class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM