简体   繁体   English

C#:分类到文件夹中

[英]C# : Classes into Folders

I want to organize my solution by adding folders and organize classes into them我想通过添加文件夹来组织我的解决方案并将类组织到其中
how to access these classes as i tried to access them but can't当我试图访问它们但不能访问这些类时如何访问它们
folders looks like packages in java文件夹看起来像 java 中的包

When you create a folder in the Visual C# project it normally creates a namespace for items created in that folder.在 Visual C# 项目中创建文件夹时,它通常会为在该文件夹中创建的项目创建命名空间 You need to add a using blah.foldername statement to the c# file where you are trying to use the items from the folder, or you can edit the file in the folder to use namespace blah instead of namespace blah.folder .您需要将using blah.foldername语句添加到 c# 文件中,您尝试在其中使用文件夹中的项目,或者您可以编辑文件夹中的文件以使用namespace blah而不是namespace blah.folder

Visual Studio mimics your project's hierarchy on the hard drive. Visual Studio 模仿您的项目在硬盘驱动器上的层次结构。 When you add a solution folder within Visual Studio, it creates a real folder under your project directory.当您在 Visual Studio 中添加解决方案文件夹时,它会在您的项目目录下创建一个真实文件夹。 Any new projects or source files you add to the solution folder in Visual Studio will default to that directory.添加到 Visual Studio 中解决方案文件夹的任何新项目或源文件都将默认为该目录。 Also, and this gets to the heart of your question , when you add a C# file, ie, class, to the solution folder, Visual Studio places it in a sub -namespace of your project.此外,这也是您问题的核心,当您将 C# 文件(即 class)添加到解决方案文件夹时,Visual Studio 会将其放置在项目的命名空间中。

For example, if your project is named MyProject , the default namespace will be MyProject .例如,如果您的项目名为MyProject ,则默认命名空间将为MyProject If you add a solution folder to MyProject called MyFolder , any new files, ie, classes added to that folder from within Visual Studio will have a default namespace of MyProject.MyFolder .如果您向MyProject添加名为MyFolder的解决方案文件夹,则任何新文件(即从 Visual Studio 中添加到该文件夹的类)都将具有MyProject.MyFolder的默认命名空间。 Thus, in order for classes in the MyProject namespace to reference classes in the MyProject.MyFolder namespace, you need to either fully qualify the class name, eg, MyProject.MyFolder.MyClass or include a using MyProject.MyFolder;因此,为了让MyProject命名空间中的类引用MyProject.MyFolder命名空间中的类,您需要完全限定 class 名称,例如MyProject.MyFolder.MyClass或包含using MyProject.MyFolder; statement at the top of file where the class is used.使用 class 的文件顶部的语句。

If you are asking about solution folders, they don't translate to the resulting code.如果您询问解决方案文件夹,它们不会转换为生成的代码。 They are merely a way to organize your projects.它们只是组织项目的一种方式。

If you are creating folders in your project to separate code files, then traditionally we would have the namespace represent the hierarchical structure of the solution/project.如果您在项目中创建文件夹来分隔代码文件,那么传统上我们将使用命名空间来表示解决方案/项目的层次结构。

Then you just include the namespaces as you normally would.然后,您只需像往常一样包含名称空间。

Does this answer your question?这回答了你的问题了吗?

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

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