0

I have 2 classes, one inherit from anotherone

public class A
{
    public string prop1 { get; set; }
    public string prop2 { get; set; }
    public string prop3 { get; set; }
}

public class B : A
{
    public string prop4 { get; set; }
}

Now I create a List of class B and cast it into a type of class B

List<B> BList = new List<B>();
BList = new List<B>() { new B() { prop1 = "1", prop2 = "2", prop3 = "3", prop4 = "4" } };
List<A> AList = BList.Cast<A>().ToList();

But when I debug, there is still a reference to class B: enter image description here

How can I remove / avoid the display of properties from the base class?

strongbutgood
  • 655
  • 7
  • 22
c0rd
  • 1,329
  • 1
  • 13
  • 20
  • 1
    Why do you want to avoid this? Is it just to make debugging easier / less cluttered? If not, then do you really need one class to inherit from the other? – Stephen Wilson Sep 21 '16 at 08:29
  • 2
    If `B` inherits from `A`, there will always be a reference from `B` to `A`. How would you want to avoid it and most importantly, why? – walther Sep 21 '16 at 08:29
  • 2
    This makes no sense at all as there is *no* reference to `A`, as `B`already *is* an `A`-instance. What you see in the debugger is just the instance of `B` casted to `A`. – MakePeaceGreatAgain Sep 21 '16 at 08:30
  • 2
    Are you trying to achieve [object slicing](http://stackoverflow.com/questions/274626/what-is-object-slicing)? That doesn't happen in .NET. – Damien_The_Unbeliever Sep 21 '16 at 08:39
  • @c0rd Is that because you want to prevent end users from accessing the properties in the base class? What reason do you want to do this? – strongbutgood Sep 21 '16 at 08:54
  • the reason is, that i pass `A` within a WCF Service and i always get the exception `Error while Deserializing B` - invalid contract type. So he recognizes somehow the wrong type – c0rd Sep 21 '16 at 08:56
  • Even if that would exist in .NET, the slice works in the opposite direction, thus when you cast a derived instance to its base-class you´re losing the information from the *derived* one. What you want is to omit the information from the *base*-class which is pure non-sense. – MakePeaceGreatAgain Sep 21 '16 at 08:56
  • @c0rd if your contract requires a B then passing A does not fulfill the contract. You could relax the contract to allow A and then add B as a KnownType via the KnownTypeAttribute. – strongbutgood Sep 21 '16 at 09:06
  • thank you so far i'll ask this in another question, thats something else – c0rd Sep 21 '16 at 10:28
  • @Damien_The_Unbeliever, HimBromBeere, walther I put this into another question since the reason was not good enough explained http://stackoverflow.com/q/39613957/6755471 – c0rd Sep 21 '16 at 11:16

1 Answers1

1

You cannot remove this but you can make it appear differently in the debugger. Use a debugger type proxy as documented here to display the class in the debugger the way you prefer. Lists and dictionaries are done this way so you could check out the source code for that using a decompiler such as ILSpy.

For example:

using System.Diagnostics;

[DebuggerTypeProxy(typeof(BDebugView))]
public class B : A
{
    private class BDebugView
    {
        private B _actualObject;
        public string prop4 { get { return _actualObject.prop4; } }

        public BDebugView(B actualObject)
        {
            this._actualObject = actualObject;
        }
    }
}

I think that should do it, I haven't tested though.

strongbutgood
  • 655
  • 7
  • 22