Is there a way to reach a `protected` member of another object from a derived type?

后端 未结 7 1171
轻奢々
轻奢々 2020-11-30 13:41
class MyBase
{
    protected object PropertyOfBase { get; set; }
}

class MyType : MyBase
{
    void MyMethod(MyBase parameter)
    {
        // I am looking for:
           


        
7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 14:26

    There's a good reason you can't do this. Suppose someone writes:

    class Other : MyBase { }
    
    new MyType().MyMethod(new Other());
    

    If the language allowed what you're asking for, you could violate the assumed invariants of Other by modifying the value of PropertyOfBase.

提交回复
热议问题