Home

Dumping of "new" property

edited April 2

Is it possible to Dump the new property of the same name of an inherited class?

void Main()
{
    var data = new ChildClass();
    ((BaseClass)data).Id = "myentity";
    data.Dump();
}

public class BaseClass /* generated class so I don't want to modify it */
{
    public string Id { get; set; }
}

public class ChildClass : BaseClass
{
    public new Hyperlinq Id => new Hyperlinq($"https://localhost/entities/{base.Id}", base.Id);
}

The dump shows the plain string. I would like to show the hyperlink instead. The above is just example I know I can add custom ToDump() implementation but the BaseClass has many properties so I have to effectively duplicate all of them and maintain changes. Would be nice to avoid it.

Comments

  • Ok, came with relatively usable solution. Still I think the Dump should by default prefer the "new" property value over the base class.

    public class ChildClass : BaseClass
    {
        public new Hyperlinq Id => new Hyperlinq($"https://localhost/entities/{base.Id}", base.Id);
    
        object ToDump() => AsExpandoObject(this);
    }
    
    public static ExpandoObject AsExpandoObject(object source)
    {
        var result = new ExpandoObject();
        var resultDictionary = (IDictionary<string, object>)result;
    
        foreach (var prop in source.GetType().GetProperties())
            resultDictionary.TryAdd(prop.Name, prop.GetValue(source));
    
        return result;
    }
    
  • Good call - the member on the derived type should take precedence. This should now be fixed in the 8.3.5 beta:
    https://www.linqpad.net/linqpad8.aspx#beta

Sign In or Register to comment.