为什么我不能访问C#protected成员除了这样吗? [英] Why can't I access C# protected members except like this?

查看:112
本文介绍了为什么我不能访问C#protected成员除了这样吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code:

abstract class C
{
    protected abstract void F(D d);
}

class D : C
{
    protected override void F(D d) { }

    void G(C c)
    {
        c.F(this);
    }
}

生成此错误:

透过型'C'的预选赛无法访问受保护的成员C.F(D)';预选赛的类型必须是'D'(或其衍生)

Cannot access protected member 'C.F(D)' via a qualifier of type 'C'; the qualifier must be of type 'D' (or derived from it)

在世界上他们在想什么? (可否改变这一规则碰坏?),是有办法解决除了使˚F公开?

What in the world were they thinking? (Would altering that rule break something?) And is there a way around that aside from making F public?


编辑:我现在得到了为什么这是由于原因(<一个href=\"http://stackoverflow.com/questions/567705/why-cant-i-access-c-protected-members-except-like-this/567732#567732\">Greg)但我还是有点困惑,理性;给出:

I now get the reason for why this is (Thanks Greg) but I'm still a bit perplexed as to the rational; given:

class E : C
{
    protected override void F(D d) { }
}

为什么的不应该的ð能够能够调用E.F?

Why shouldn't D be able to be able to call E.F?


编辑错误信息,所以我可能已经把一个错字在那里。

The error message is edited so I might have put a typo in there.

推荐答案

在保护的关键字意味着只有一种类型和种类从该类型派生可以访问该成员。 D有到C没有任何关系,因此无法访问的成员。

The "protected" keyword means that only a type and types that derive from that type can access the member. D has no relationship to C therefore cannot access the member.

您有几个选择,如果你希望能够访问该成员

You have a couple of options if you want to be able to access that member


  • 请其公开

  • 请它的内部。这将允许任何类型相同的装配体中的成员(或其他组件时,应当在添加朋​​友的)

  • 从C派生ð

修改

这情景在C#规范第3.5.3叫出来。

This scenario is called out in section 3.5.3 of the C# spec.

这是不允许的原因是因为它允许跨层次调用。试想一下,除了D,还有一个基类的C叫E.如果code可以编译它将使Ð访问该成员EF这种类型的场景是不是在C#中允许的(我的相信的CLR的,但我不100%知道)。

The reason this is not allowed is because it would allow for cross hierarchy calls. Imagine that in addition to D, there was another base class of C called E. If your code could compile it would allow D to access the member E.F. This type of scenario is not allowed in C# (and I believe the CLR but I don't 100% know).

EDIT2 为什么这是不好的。

警告,这是我的看法。

这是现在允许的原因是它使我们来思考一类的行为非常困难。访问修饰符的目标是给了谁可以完全访问具体方法开发人员控制。试想一下,下面的类

The reason this is now allowed is it makes it very difficult to reason about the behavior of a class. The goal of access modifiers is to give the developer control over exactly who can access specific methods. Imagine the following class

sealed class MyClass : C {
  override F(D d) { ... } 
}

考虑一下,如果F是一个有点时间关键功能会发生什么。与当前的行为,我可以推理我班的正确性。毕竟,只有两种情况下MyClass.F将被调用。

Consider what happens if F is a somewhat time critical function. With the current behavior I can reason about the correctness of my class. After all there are only two cases where MyClass.F will be called.


  1. 其中,在C的调用

  2. 当我明确地调用它MyClass中

我可以检查这些电话和前来如何MyClass的功能,合理的结论。

I can examine these calls and come to a reasonable conclusion about how MyClass functions.

现在,如果C#确实允许跨层次保护访问我可以让没有这样的保证。任何人在一个完全不同的组件可得的和C.派生然后,他们可以随意调用MyClass.F。这使得它完全不可能来思考我的课的正确性。

Now, if C# does allow cross hierarchy protected access I can make no such guarantee. Anyone in a completely different assembly can come by and derive from C. Then they can call MyClass.F at will. This makes it completely impossible to reason about the correctness of my class.

这篇关于为什么我不能访问C#protected成员除了这样吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆