简体   繁体   English

标头-类似于C#中的文件

[英]header - like files in C#

Is there a way to 'fake' header files in C# using namespaces? 有没有一种方法可以使用命名空间在C#中“伪造”头文件? From what I've heard, there is no way to get the preprocessing that they provided in C++; 据我所知,无法获得它们在C ++中提供的预处理。 but I'd like the organization aspect of it rather than wrapping classes in namespaces and relying on an IDE to tell me whats in what . 但是我希望它的组织方面,而不是将类包装在命名空间中,而是依靠IDE告诉我到底是什么

for example (note the incorrect syntax) suppose 例如(注意错误的语法)

ns1.cs
iamaclass.cs
iamanotherclass.cs
yaclass.cs
yaaclass.cs

where ns1.cs 's contents are: 其中ns1.cs的内容是:

namespace ns1
{
   extern class iamaclass;
   extern class iamanotherclass;

   namespace ns1-nested
   {
      extern class yaclass
      extern class yaaclass
   }
}

If you make the directory structure of your projects match the namespace structure, this goes a long way toward getting the organizational benefits. 如果使项目的目录结构与名称空间结构相匹配,这将对获取组织收益大有帮助。 Now your project file and associated directory structure contains all of the information proposed in your 'header' file. 现在,您的项目文件和关联的目录结构包含“标头”文件中建议的所有信息。

There is no way to do this in C#. 用C#无法做到这一点。 The closest you could get is to make your ns1.cs file and fill it with partial class .... , but that is really pointless. 您可以获得的最接近的结果是制作ns1.cs文件,并用partial class ....填充它,但这确实没有意义。 At that point you should just write a document as you won't really get any compiler help in enforcing the structure. 到那时,您应该只写一个文档,因为在执行该结构时并不会得到任何编译器帮助。

I would say this is a case of "forget how you did it in C++". 我会说这是“忘记在C ++中的工作方式”的情况。 Usually it's not important to find out what's inside a namespace, but more important to figure out what namespace a certain class is in, and C#'s organization will show you that perfectly. 通常,找出名称空间内的内容并不重要,而找出某个类所在的名称空间更重要,C#的组织会完美地向您展示。

Just a small last thing: is there a practical reason you don't want to rely on an IDE, or is it just a nitpick? 只是最后一小件事:是否有实际的原因您不想依赖IDE,还是仅仅是nitpick? It's OK to rely on the help of an IDE for much development. 可以依靠IDE的帮助进行大量开发。

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

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