简体   繁体   English

在使用时将类名=名称空间

[英]Putting a class name = namespace, in using

using BusinessTask =  MyProject.WinService.Tasks.Core.BusinessTask;

I understand from the above BusinessTask class can now be used, however I'm wondering what reasons perhaps the author didn't just put: 我从上面的BusinessTask类可以理解,现在可以使用它,但是我想知道作者可能没有提出什么原因:

using MyProject.WinService.Tasks.Core;

Is it perhaps to be selective on the exposure of that location since "Core" has many other classes? 由于“核心”还有许多其他类别,因此是否应该对该位置的曝光进行选择?

BusinessTask may exist in several namespaces. BusinessTask可能存在于几个名称空间中。 To avoid full namespace path specification near each BusinessTask usage you can explicitly say: 为了避免在每个BusinessTask用法附近使用完整的名称空间路径规范,您可以明确地说:

using BusinessTask =  MyProject.WinService.Tasks.Core.BusinessTask;

and compiler will know that when you are using BusinessTask it should pick it up from MyProject.WinService.Tasks.Core namespace. 并且编译器将知道,当您使用BusinessTask时,应从MyProject.WinService.Tasks.Core命名空间中进行选择。

Here is a great example How to handle same class name in different namespaces? 这是一个很好的示例, 如何在不同的命名空间中处理相同的类名?

You can use a class name alias to avoid name collisions when there is a class in another imported namespace with the same name. 当另一个导入的名称空间中有一个同名的类时,可以使用类名别名来避免名称冲突。 Even if you simply call the alias the same name as the target class, it teaches the compiler that every time you use your class name within that scope, you're referring to the one you've aliased. 即使您只是简单地将别名命名为与目标类相同的名称,它也会告诉编译器,每次在该范围内使用您的类名时,您都是在指代别名。

See this answer here: https://softwareengineering.stackexchange.com/a/170839/144365 请在此处查看此答案: https : //softwareengineering.stackexchange.com/a/170839/144365

One case we faced is that we have a class name defined in two namespaces the we are referencing in the same class. 我们遇到的一种情况是,我们在同一类中引用的两个名称空间中定义了一个类名。 So we had to use the Fully Qualified name, so the code turned out to be: 因此,我们必须使用“完全合格”名称,因此代码原来是:

public CompanyName.ProjectName.FeatureName.ClassName SomeMethod(object param1, 
                                                              object param2, object param3)
{


}

Which was very unclear. 哪个还不清楚。 So we replace that with 所以我们用

using SomeIdentifier = CompanyName.ProjectName.FeatureName;

Then the method became: 然后该方法变为:

public SomeIdentifier.ClassName SomeMethod(object param1, object param2, object param3)
{


}

Having use that type like 10 times in this class, making use of that alias in the using made the code more readable. 在该类中使用该类型大约10次,在using中使用该别名使代码更具可读性。

Assume in your case you need to reference a library defining a type named 'Task'. 假设您需要引用定义了“任务”类型的库。 If you need to use that inside a classing referencing System.Threading.Tasks . 如果需要在引用System.Threading.Tasks的分类中使用它。 You need to either use the fully Qualified name System.Threading.Tasks task = .... or define an alias. 您需要使用全限定名System.Threading.Tasks task = ....或定义别名。

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

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