如何让C#命名空间的工作,如Java包,这样它们会自动移动它们时重命名? [英] How to make the C# namespace work like Java Packages so they rename automatically when moving them?

查看:201
本文介绍了如何让C#命名空间的工作,如Java包,这样它们会自动移动它们时重命名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自Java和看到包在Java中非常方便。当您移动类到另一个包时,它会自动更改包。 (当然,通过IDE,比如Eclipse或Netbean)

I come from Java and see that package in Java is very convenient. When you move a class to another package, it will change automatically the package. (of course, by IDE such as Eclipse or Netbean)

不过,C#使用命名空间和没有我的空间改名自动像它在Java中。比如我有一个文件,该文件命名空间是 com.app ,我把它放在 com.app ,但后来一次,我移动这个文件到 com.lib 文件夹和其命名空间仍然 com.app 。所以,我觉得这是很难管理,因为我手动移动它。

But C# is using namespace and don't have my namespace renamed automatically like it does in Java. For example I have a file which namespace is com.app and I put it in com.app, but at later time, I move this file to com.lib folder and its namespace still be com.app. So, I find this is difficult to manage because I'm moving it manually.

请给我如何解决我的问题有所帮助。 (文件的命名空间被命名文件夹包含,而当我移动到其他,我将自动更改)。我们能做到吗?

Please give me help in how to fix my problem. (that namespace of file is named by folder it contains, and when I move to other, I will automatically change). Can we do it?

推荐答案

我用所谓的 ReSharper的。 (其中的很多很多有用的功能),当一个命名空间是错误的(基于组件的文件夹层次结构和根命名空间),并且可以给你办到它突出。

I fix the problem by using an IDE plugin called Resharper. (Among many, many useful features) it highlights when a namespace is wrong (based on the folder hierarchy and root namespace of the assembly) and can fix it for you.

请注意,不像在Java中,有时有非常正当的理由一类是在一个命名空间中的其他的比通过目录结构推断。一个很好的例子可能是扩展方法类,需要在范围,即调用它们的类。因此,这是常见的有:

Note that unlike in Java, there are sometimes very valid reasons for a class to be in a namespace other than the one inferred by the directory structure. A good example might be extension method classes, which need to be in scope in the class that is invoking them. Therefore it is common to have:

/myProject
  /extensions
     /MyExtensionMethodClass.cs

myProject的命名空间(以便扩展方法在myProject的任何地方使用无使用指令)

with a namespace like myProject (so that the extension methods can be used anywhere in myProject without a using directive)

这篇关于如何让C#命名空间的工作,如Java包,这样它们会自动移动它们时重命名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆