简体   繁体   English

全局命名空间C#中的命名空间

[英]Namespaces inside a global namespace C#

So I have made a .dll containing some methods that I wish to use in another project. 因此,我制作了一个.dll其中包含一些我想在另一个项目中使用的方法。 All is going well. 一切进展顺利。 I currently have the .cs files inside the .dll set out like this: 我目前在.dll.cs文件设置如下:

GeneralClass.cs
NetworkingClass.cs
TextProcessingClass.cs

And inside each of them is this: 在每个人里面是这样的:

namespace General // The name of each .cs file without 'Class'
{
    ...
}

So I will have these namespaces: 因此,我将拥有以下名称空间:

  • General

  • Networking

  • TextProcessing

And I can access them in another project by doing: 我可以通过执行以下操作在另一个项目中访问它们:

using General;
using Networking;
...

This is all fine but I was wondering if there was a way to do it like this: 一切都很好,但我想知道是否有办法做到这一点:

using MyDll.General;
using MyDll.Networking;
...

So everything would be under MyDll , just like System and all of its sub namespaces. 因此,所有内容都将在MyDll下,就像System及其所有子名称空间一样。

If you can help me, please post here. 如果可以帮助我,请在此处发布。

将名称空间定义为:

namespace MyDll.General

If you want namespace to be different why not to just do it? 如果您希望命名空间有所不同,为什么不这样做呢? There are no restrictions on namespace to match file name (unlike some other languages). 命名空间与文件名匹配没有任何限制(不同于其他语言)。

namespace MyDll.General 
{ 
    ... 
} 

Change you namespace like 像这样更改您的名称空间

namespace MyDll.General
{
    // define classes here
}

namespace MyDll.Networking
{
    // define classes here
}

namespace MyDll.TextProcessing
{
    // define classes here
}

In your dll project, just change 在您的dll项目中,只需进行更改

namespace General {}

to

namespace MyDll.General {}

and re-build it 并重建它

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

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