How to avoid having the same name for a class and it#39;s namespace, such as Technology.Technology?(如何避免一个类和它的命名空间同名,例如 Technology.Technology?)
问题描述
我经常在命名空间和该命名空间中的类之间发生命名冲突,并且想知道在首先使用这些名称似乎有意义时处理此问题的最佳实践,除了添加随机前缀.
I am frequently having naming conflicts between a namespace and a class within that namespace and would like to know a best practice for handling this when it seems to make sense to use these names in the first place, besides adding random prefixes.
拥有一个 Models.Technology 命名空间似乎很有意义,其中包含数十个技术类(即武器、农业、回收技术)和一些相关接口.我还决定在 Technology 命名空间中创建一个 Technology 抽象类,所有技术都从该类派生.
It seemed to make sense to have a Models.Technology namespace, with dozens of technology classes (ie. Weapon, Agricultural, Recycling technologies) and some related interfaces in it. I also decided to have a Technology abstract class, in the Technology namespace, from which all technologies derive.
但是,这迫使我使用这样的代码:
However, this is forcing me to use code like this:
public Technology.Technology research(Technology.Technology tech) {...}
同样:
public Building.Building build(int count) {...}
顺便说一下,我没有命名我的命名空间 Technologies,因为我在其他地方使用该术语作为可用技术列表的包装器......
By the way I did not name my namespace Technologies since I use that term elsewhere as a wrapper for a list of available technologies...
推荐答案
如果没有更准确的指示来说明您何时遇到此问题,很难给您一些一般性的建议.
Without more precise indications on when you encounter this problem it's hard to give you some general advice.
但是我会假设当您的命名空间中有一个基类并且其他类从它继承时,您会遇到这个问题.如果是这种情况,您可以调用您的类 BaseTechnology(基类)或 AbstractBuilding(抽象类)或 IAnimal(接口).这甚至可以让您更加清晰,因为您在类型的名称中指定了一些重要的内容.
But I'm gonna suppose you have this problem when you have a base class in your namespace, with others classes inheriting from it. If that's the case you could call your class BaseTechnology (base class) or AbstractBuilding (abstract class) or IAnimal (interface). This even give you more clarity since you specify in the name of your type something important about it.
这篇关于如何避免一个类和它的命名空间同名,例如 Technology.Technology?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何避免一个类和它的命名空间同名,例如 Technology.Technology?
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01