- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要覆盖 Equals
可访问性为 GetHashcode
的嵌套类中的运算符(以及因此的 internal
方法) .
当我尝试这样做时,编译器提示我无法重写等于 - a public
成员 - 带有 private
方法。但我无法覆盖 public
,因为类本身是 internal
, 和嵌套。
如何做到这一点?我怎样才能覆盖 Equals
和 GetHashcode
在一个不是 public
的类中(事实上,是嵌套和 internal
)?
最佳答案
I'm not sure why it works, though.
此行为在 C# 规范的第 3.5.2 节中定义:
The accessibility domain of a nested member
M
declared in a typeT
within a programP
is defined as follows (noting that M itself may possibly be a type): [...]
- If the declared accessibility of
M
isinternal
, the accessibility domain ofM
is the intersection of the accessibility domain ofT
with the program text ofP
.
此规定要求对于具有内部可访问性的类,类型成员可以等效地标记为公共(public)或内部。
You can't have a member whose access level is higher than it's containing class?
这是准确的;规范的同一部分指出“成员的可访问域永远不会比声明成员的类型的可访问域更具包容性”。重要的是要认识到,虽然语言允许具有内部可访问性的类型的成员声明为公共(public)的,但它仍然具有内部可访问性(就像它被声明为内部一样)。
换句话说,在下面的代码段中,X
的可访问性在两行中是等效的:
internal class A {public void X() {}}
internal class A {internal void X() {}}
关于c# - 覆盖嵌套内部类中的运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24472960/
我是一名优秀的程序员,十分优秀!