Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

c# - overriding protected internal with protected!

This is an extension for this question asked an hour ago.

We cannot modify the access modifiers, when overriding a virtual method in derived class. Consider Control class in System.Web.UI namespace

public class Control : IComponent, IDisposable,...
{ 
   protected internal virtual void CreateChildControls()
   { }
   .
   .
}

Now Consider This

public class someClass : System.Web.UI.Control
    { 
       // This should not compile but it does
        protected override void CreateChildControls()
        { }

       // This should compile but it does not
        protected internal override void CreateChildControls()
        { }  
    }

can any body explain this ? Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

We cannot modify the access modifiers when overriding a virtual method in derived class.

That statement is false. You can and must change the access modifiers when in precisely the situation you describe. In other situations you must not change the access modifiers.

I refer you to section 10.6.4 of the specification, which states:

an override declaration cannot change the accessibility of the virtual method. However, if the overridden base method is protected internal and it is declared in a different assembly than the assembly containing the override method then the override method’s declared accessibility must be protected.

The reasoning is straightforward.

You, Asad, have a bank account, BankAccount.

You have a House. You rent a room in House to your best friend Charlie.

Charlie has a son, David, who lives in an Apartment.

You have a son, Elroy, who lives in a Condo.

Elroy has a son, your grandson, Frank, who lives in a Yurt.

Elroy has a best friend Greg who lives in the Condo with him.

You grant access to your BankAccount to yourself, to anyone living in House, and to any of your descendents. So the people who can access the bank account are Asad, Charlie, Elroy, and Frank.

David does not get access because he is neither you, nor your descendent, nor is he living in House. That he is a child of your housemate is irrelevant; he doesn't get access to your BankAccount.

Greg does not get access to your bank account either. He is not your descendent. He does not live in House. The fact that he is living with your descendent does not grant him the same rights as your descendent.

Now we come to the crux of the matter. Elroy is not allowed to extend access to your BankAccount to Greg. You own that BankAccount, and you said "myself, my descendents and my housemates". Your children don't have the right to extend the accessibility of BankAccount beyond what you initially set up.

When Elroy describes what access he has to BankAccount, he is only allowed to say "I grant access to this to myself and my descendents", because that is what you already allowed. He cannot say "I grant access to BankAccount to myself, my descendents and to the other residents of Condo".

Just to be clear:

  • I and my descendents get access = protected access
  • I and my housemates get access = internal access
  • I and my descendents and my housemates get access = protected internal access
  • Control = Asad
  • CreateChildControls = BankAccount
  • House = System.Web.DLL
  • Charlie = any type in System.Web.DLL
  • David = derived type of Charlie in assembly Apartment.DLL
  • Elroy = someClass
  • Condo = your assembly containing SomeClass
  • Greg = some other class in Condo.DLL
  • Frank = derived type of someClass in Yurt.DLL
  • Yurt = some other assembly

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...