简体   繁体   English

C#.NET 项目如何使用点表示法确定依赖关系?

[英]How do C#.NET projects determine dependencies with dot notation?

I am being introduced to the C# language (pretty sure it's .NET framework also but this may be irrelevant) at my work.在我的工作中,有人向我介绍了 C# 语言(很确定它也是 .NET 框架,但这可能无关紧要)。 I am using Jetbrains/Rider as my IDE (however this is also probably irrelevant).我使用 Jetbrains/Rider 作为我的 IDE(但这也可能无关紧要)。

My question is an obvious one to anyone looking at the code.对于任何查看代码的人来说,我的问题都是显而易见的。 I see folders which have dots in the name.我看到名称中带有点的文件夹。 I also see that the references to these files in the code use dot notation to denote the file hierarchy.我还看到代码中对这些文件的引用使用点符号来表示文件层次结构。 My question is, what happens when there is a conflict which is definitely possible given this structure.我的问题是,在这种结构下绝对有可能发生冲突时会发生什么。

Here is an example: (a made up one not from our code)这是一个示例:(不是来自我们的代码的编造)

Actual file structure (indentations denote sub-folders, f denotes folder):实际文件结构(缩进表示子文件夹,f 表示文件夹):

MyApp (f)
  Main (f)
    Stuff.cs
  Core.cs
MyApp.Main (f)
  Stuff.cs
MyApp.Main.Stuff (f)
  MoreStuff.cs

This file structure is obviously ridiculous but I see no reason why it coudn't be done this way.这种文件结构显然很荒谬,但我看不出为什么不能这样做。 Now what if you reference the Stuff.cs file this way现在,如果您以这种方式引用 Stuff.cs 文件会怎样

using MyApp.Main.Stuff;

namespace MyApp.Main
{
  //whatever
}

How does C# resolve this? C#如何解决这个问题?

The folder structure and file names don't matter.文件夹结构和文件名无关紧要。 The namespaces and class names are what matter.命名空间和类名才是最重要的。

There are some exceptions, like the App_Data and App_Code folders, and Global.asax file in ASP.NET, and the Main method in console apps.有一些例外,例如App_DataApp_Code文件夹,ASP.NET 中的Global.asax文件,以及控制台应用程序中的Main方法

However, matching the class name to the file name and the namespace to the folder structure makes it much easier to find the right file in your project, particularly in bigger projects.但是,将类名与文件名相匹配,将命名空间与文件夹结构相匹配,可以更轻松地在项目中找到正确的文件,尤其是在较大的项目中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM