get hidden derived property from base class

52 Views Asked by At

The classA has property pA which has type of baseType.

I have created classB derived from classA and it is hiding pA property with "new" modifier: newType pA;

newType derived from baseType...

How can I refer to the classB pA in baseType: In classA the instance of classB has null value of pA, but in classB pA has the initiated value.

I would like to call a classA base method using the value of classB's pA!

override is working, but for that I have to use baseType: public override baseType pA;

using "new" is better then I can use newType: public new newType pA;

Or I can do declaration + override:

public newType pB { get; set; }
public override baseType pA { get => pB; }

Above is working, but better would be to use "new" and in the base type pA should get the value from the descendant classB:

public virtual baseType pA {
get { return GetType().GetProperties().Where(t => t.Name == "pA" && !(t.DeclaringType is classA)).First().GetValue(this) as baseType... }
set {...} }

The better solution also working, but checking the DeclaringType has an issue: GetType().GetProperties().Where(t => t.Name == "pA") will return with 2 properties and only one is for classB's pA property.

Similar issue: Accessing outer class hidden...

The referenced article is not solution to my problem. Cannot use ((classB)this).pA in classA. Don't want to create that dependency. When I am creating classC inherited from classA then it will not work...

I have already sorted out the DeclaringType issue:

In classA I have defined pA like this:

public virtual baseType pA {
        get {
            var p = GetType().GetProperties().Where(t => t.Name == "pA" && !(t.DeclaringType.Equals(typeof(classA))));
            if (p.Count() > 0) return p.First().GetValue(this) as baseType; else return _pA;
        }
            set => _pA = value;
    }

So I can overwrite the pA property with new type In classB:

public new newType pA { get; set; }

Then classA's hidden pA property will be replaced with classB's property. When callin the baseMethod(pA) in classA the pA's value will be the value from classB. classA pA type is baseType classB pA type is newType...

0

There are 0 best solutions below