Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

oop

class MyBase
{
    protected object PropertyOfBase { get; set; }
}

class MyType : MyBase
{
    void MyMethod(MyBase parameter)
    {
        // I am looking for:
        object p = parameter.PropertyOfBase;  // error CS1540: Cannot access protected member 'MyBase.PropertyOfBase' via a qualifier of type 'MyBase'; the qualifier must be of type 'MyType' (or derived from it)
    }
}

Is there a way to get a protected property of a parameter of a type from an extending type without reflection? Since the extending class knows of the property through its base type, it would make sense if possible.

like image 366
Serhat Ozgel Avatar asked Dec 05 '08 16:12

Serhat Ozgel


People also ask

How do you access protected members in a derived class?

A class in C++ has public, private and protected sections which contain the corresponding class members. Protected members in a class are similar to private members as they cannot be accessed from outside the class. But they can be accessed by derived classes or child classes while private members cannot.

Can we access protected member derived class in Java?

We can access protected members of a class in another class that is present in the same package.

Can derived classes be access protected?

A derived class cannot access protected members of its base class through an instance of the base class. An instance of the base class declared in the derived class might, at run time, be an instance of another type that is derived from the same base but is not otherwise related to the derived class.

How do I access protected data members?

The data members and member functions declared public can be accessed by other classes too. The class member declared as Protected are inaccessible outside the class but they can be accessed by any subclass(derived class) of that class.


2 Answers

Last time I faced a similar problem, I used the solution of adding a protected static method to the base:

class MyBase
{
    protected object PropertyOfBase { get; set; }

    protected static object GetPropertyOfBaseOf(MyBase obj) 
    {
        return obj.PropertyOfBase;
    }
}

class MyType : MyBase
{
    void MyMethod(MyBase parameter)
    {
        object p = GetPropertyOfBaseOf(parameter);
    }
}
like image 198
Medinoc Avatar answered Sep 28 '22 04:09

Medinoc


No, you can't do this.

You're only allowed to access protected members of objects of the accessing type (or derived from it). Here, we don't know whether the parameter is of type MyType or SomeOtherCompletelyDifferentType.

EDIT: The relevant bit of the C# 3.0 spec is section 3.5.3:

When a protected instance member is accessed outside the program text of the class in which it is declared, and when a protected internal instance member is accessed outside the program text of the program in which it is declared, the access must take place within a class declaration that derives from the class in which it is declared. Furthermore, the access is required to take place through an instance of that derived class type or a class type constructed from it. This restriction prevents one derived class from accessing protected members of other derived classes, even when the members are inherited from the same base class.

like image 41
Jon Skeet Avatar answered Sep 28 '22 06:09

Jon Skeet