CA1034:嵌套类型不应可见
这里是 我试图理解的规则的解释。以下是代码分析器抱怨的简化代码:
Public Class CustomerSpeed
Public Enum ProfitTypeEnum As Integer
NotSpecified = 0
FlatAmount = 1
PercentOfProfit = 2
End Enum
Private _ProfitTypeEnum As ProfitTypeEnum
Public Sub New(ByVal profitType As ProfitTypeEnum)
_ProfitTypeEnum = profitType
End Sub
End Class
如果枚举仅与类相关,为什么将其设为类中的包含类型是一件坏事?对我来说似乎更简洁...
有人知道下面这行是什么意思吗?:
嵌套类型包括成员可访问性的概念,一些程序员并不清楚
使用命名空间对类和枚举进行分组似乎不是解决此警告的有用方法,因为我希望两个枚举都属于与类名相同的父级别。
Here's an explanation of the rule that that I am trying to understand. Here's the simplified code that the Code Analyzer was complaining about:
Public Class CustomerSpeed
Public Enum ProfitTypeEnum As Integer
NotSpecified = 0
FlatAmount = 1
PercentOfProfit = 2
End Enum
Private _ProfitTypeEnum As ProfitTypeEnum
Public Sub New(ByVal profitType As ProfitTypeEnum)
_ProfitTypeEnum = profitType
End Sub
End Class
If the enum pertains only to the class, why is it a bad thing to make it a contained type within the class? Seems neater to me...
Does anyone know what is meant by the following line?:
Nested types include the notion of member accessibility, which some programmers do not understand clearly
Using Namespaces to group the Class and Enum doesn't seem like a useful way to resolve this warning, since I would like both the enum to belong to the same parent level as the class name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了其他响应中已经涵盖的可用性和可发现性问题之外,还存在潜在的可维护性问题。当您发现您的枚举在其他地方也可能有用时会发生什么?将其移出其父类将是一个重大更改,复制它会引入其自身的可维护性问题,并且要求 API 使用者将其与另一个类一起使用开始变得丑陋。为什么不系统地避免嵌套枚举来避免这些潜在的问题呢?
In addition to the useability and discoverability issues already covered in other responses, there is also a potential maintainability issue. What happens on the day that you discover that your enum is also potentially useful elsewhere? Moving it out of its parent class would be a breaking change, copying it would introduce its own maintainability problems, and requiring API consumers to use it with another class starts to get ugly. Why not avoid these potential problems by systematically avoiding nested enums?
枚举通常不放置在使用它们的类中,因此人们不习惯指定枚举的位置:
将枚举放置在类之外使其更易于使用:
枚举仍然包含在与类相同的命名空间中。正如分析解释所指出的,您应该使用命名空间对公共成员进行分组,而不是将它们相互嵌套。
Enumerations are not usually placed in the class that uses them, so people are not used to having specify where the enumeration is:
Placing the enumeration outside the class makes it easier to use:
The enumeration is still contained in the same namespace as the class. As the analysis explanation points out, you should use namespaces to group public members rather than nesting them in each other.
我猜这表明嵌套类型可能与静态变量混淆,因为它们都会出现在“.”之后的智能感知(自动完成)中。
即使你看到.NET的BCL(基类库),所有的枚举,即使它们只在一个类中使用,它们也不是嵌套的,原因是,当你想要比较或实例化时,类名+“。 ” + 枚举名称会令人困惑,如下所示,微软也说它会让人困惑,这是正确的。
-- 示例
一个是类型,另一个是变量,大多数 MS Code 分析器警告都是为了更好的设计,但是 @George 有一个很大的例外,如果您不喜欢它,请不要使用它,只需继续并禁用警告即可。您当然可以使用大类名而不是命名空间,这只是您的选择。但良好的编程技能是关于别人如何看待你的代码而不是你喜欢什么!
命名空间是用来组织的,通过组织,我们可以减少打字,这就是用更少的努力做更多的工作。但如果您喜欢输入大名鼎鼎的名字,那么没有人会阻止您。
I guess it indicates that nested type can be confused with static variable because they both will appear in intellisense (auto complete) after the ".".
Even if you see BCL (Base Class Library) of .NET, all the enums, even if they are only used in one class, they are not nested, reason is, when you want to compare or instantiate, the class names + "." + enum names will be confusing as shown below, also Microsoft is right that it will confuse people.
-- Example
One is a type and other is variable, most MS Code Analyzer warnings are for better design, however it has an big exception for @George, if you dont like it, dont use it, just go ahead and disable the warnings. You can certainly use big class names instead of namespaces, its simply your choice. But good programming skills is about how others percieve your code rather then what you like !!!
And namespaces are there to organize and by organizing we type less, thats what all about doing more work with less efforts. But if you like big names to type, no one is stopping you.