如何在不同的命名空间处理同一类的名字吗?名字、不同、如何在、空间

2023-09-04 08:24:31 作者:套路撩人

我想创建一个公共库结构。我对每一个普通的lib我想创建单独的项目,这样做

我有以下2命名空间: MyCompany.ERP 和 MyCompany.Bar code

我需要他们两个人有一个类名为管理工具,然后是静态的。如果我这样做,我一定要去我之前静态类指定以访问完整的命名空间名称。

有没有其他的preferred办法做到这一点?

或者我应该去为不同的名称,在课堂上喜欢的吧codeUtils 和 ERPUtils

解决方案   

如果我这样做,我一定要去我之前静态类指定以完整的命名空间的名称来访问它?

没有,就没有必要的是,虽然详细信息取决于将使用这些类型和 使用声明该有。

命名空间中 不明确 不同dll调用相同命名空间 修改命名空间名称 解决方案

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

如果您同时使用的命名空间,你将不得不完全限定用途,或使用命名空间/类型的别名以消除歧义的类型。

 使用ERPUtils = MyCompany.ERP.Utilities;
使用BCUtils = MyCompany.Bar code.Utilities;

公共无效的MyMethod()
{
  变种一个= ERPUtils.Method();
  变种B = BCUtils.Method();
}
 

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.ERP and MyCompany.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.

Is there any other preferred way to do it?

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

解决方案

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 using declarations it has.

If you only use one of 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 aliases to disambiguate the types.

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

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