[英]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.