C# 如何在不同的命名空间中处理相同的类名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13345037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:16:16  来源:igfitidea点击:

How to handle same class name in different namespaces?

c#.netprojects-and-solutions

提问by e4rthdog

I am trying to create a common library structure. I am doing this by creating separate projects for every common lib i want

我正在尝试创建一个通用的库结构。我通过为我想要的每个公共库创建单独的项目来做到这一点

I have the following 2 namespaces: MyCompany.ERPand MyCompany.Barcode

我有以下 2 个命名空间:MyCompany.ERPMyCompany.Barcode

I need both of them to have a class named "Utilities"and be static. If i do that i will then need to specify the full namespace name before my static class in order to access it.

我需要他们都有一个名为“Utilities”的类并且是静态的。如果我这样做,我将需要在我的静态类之前指定完整的命名空间名称才能访问它。

Is there any other preferred way to do it?

有没有其他首选的方法来做到这一点?

Or i should go for different names in classes like BarcodeUtilsand ERPUtils?

或者我应该在BarcodeUtilsERPUtils等类中使用不同的名称?

采纳答案by Oded

If i do that i will then need to specify the full namespace name before my static class in order to access it?

如果我这样做,我将需要在我的静态类之前指定完整的命名空间名称才能访问它?

No, there is no need for that, though the details depend on the class that will use these types and the usingdeclarationsit has.

不,没有必要,尽管细节取决于将使用这些类型的类及其具有的using声明

If you only use oneof the namespaces in the class, there is no ambiguity and you can go ahead and use the type.

如果你只使用一个在类的命名空间,没有歧义,你可以继续使用类型。

If you use both of the namespaces, you will either have to fully qualify the usages, or use namespace/type aliasesto disambiguate the types.

如果您使用这两个命名空间,则必须完全限定用法,或者使用命名空间/类型别名来消除类型的歧义。

using ERPUtils = MyCompany.ERP.Utilities;
using BCUtils = MyCompany.Barcode.Utilities;

public void MyMethod()
{
  var a = ERPUtils.Method();
  var b = BCUtils.Method();
}

回答by Oded

You can use an alias:

您可以使用别名:

using BarcodeUtils  =  MyCompany.Barcode.Utilities;

on the pages you have clashes. But ideally rename them if this is happening in a lot of places.

在您有冲突的页面上。但如果这种情况在很多地方发生,最好重命名它们。

回答by Wouter de Kort

You will have to use the full path when both are named the same. Otherwise you will get an ambiguous reference error.

当两者名称相同时,您将必须使用完整路径。否则你会得到一个模棱两可的参考错误。

You can use an aliashowever that will save you some typing:

您可以使用别名,但是这样可以节省一些输入:

using Project = PC.MyCompany.Project;

I would go for a different name that's somewhat more descriptive. A

我会选择一个更具描述性的不同名称。一种

回答by Matt Varblow

I would suggest using different class names. If you really want to call both of them Utilities then you could use the alias feature on the using directive, e.g.

我建议使用不同的类名。如果你真的想调用它们两个 Utilities 那么你可以在 using 指令上使用别名功能,例如

using ERP = MyCompany.ERP;
using Barcode = MyCompany.Barcode;

...
    ERP.Utilities.SomeMethod();
    Barcode.Utilities.SomeMethod();

回答by Johnny_D

There isn't any other way. You can make an aliases in usingdirectives:

没有其他办法。您可以在using指令中创建别名:

using MC=MyCompany.ERP;
using MB=MyCompany.Barcode;
...
public void Test()
{
  var a = MC.Utilities.Method();
  var b = MB.Utilities.Method();
}

It's the simplest way to manage them.

这是管理它们的最简单方法。

回答by Matthew Watson

"Utilities" is not a very good name for a class, since it is far too generic. Therefore, I think you should rename both of them to something more informative.

“实用程序”对于一个类来说不是一个很好的名字,因为它太笼统了。因此,我认为您应该将它们都重命名为更具信息性的名称。

回答by kkocabiyik

It actually depends on the purpose of your classes. If you are going to distribute your Barcode.Utilities and ERP.Utilies seperately it is better stay like this. On the other hand, if you are going to use them onlyin same class, you may use 2. method for easiness of code.

这实际上取决于您的课程目的。如果您要单独分发 Barcode.Utilities 和 ERP.Utilies,最好保持这样。另一方面,如果你打算在同一个类中使用它们,你可以使用 2. 方法来简化代码。

回答by millimoose

The MS guidelineshave the following to say:

MS准则有以下的说:

Do not introduce generic type names such as Element, Node, Log, and Message. There is a very high probability it would lead to type name conflicts in common scenarios.

不要引入通用类型名称,例如 Element、Node、Log 和 Message。在常见情况下,它很有可能会导致类型名称冲突。

and

Do not give the same name to types in namespaces within a single application model.

不要为单个应用程序模型中的命名空间中的类型赋予相同的名称。

I concur that it's probably a good idea to use BarcodeUtilitiesand ErpUtilitiesinstead. (Unless the utility classes are not meant to be used by client code, in which case you could name them Utilitiesand make them internal.)

我同意使用BarcodeUtilitiesandErpUtilities代替它可能是一个好主意。(除非客户端代码不打算使用实用程序类,在这种情况下,您可以命名它们Utilities并使它们成为internal。)