Error: Derived Class property Dump does not reflect Object Dump values

The derived class property BaseProp1 is set to 1,
When Dump the object it shows 1,
When Debug and mouse over the property it shows 1.
When Dump the property it shows 0.

Is this expected?

class BaseClass
{
public int BaseProp1 { get; set; }
}

class DerivedClass : BaseClass
{
public new int BaseProp1 { get; set; }
public int TopProp { get; set; }
}
void Main()
{
DerivedClass dc = new DerivedClass();

dc.BaseProp1 = 1;
dc.TopProp = 3;
dc.BaseProp1.Dump("dc BaseProp1");

BaseClass bc2 = (BaseClass)dc;
bc2.Dump("bc2"); //The BaseProp1 value in the Dump table is 1

bc2.BaseProp1.Dump("BaseProp1");//DEBUG this line, when mouseover BaseProp1 value is 1
}

Comments

  • When you call bc2.Dump("bc2"), the Dump method operates on the variable you pass in, so the static type of the caller is lost. It calls GetType() on the object and enumerates its properties and fields, so what matters is the actual type of the object you pass in.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!