Accessing dynamic view within ExpandoObject

58 views Asked by At

I have the following method to create this ExpandoObject:

List<object> example = new List<object>();
foreach (var rate in rates)
{
    var rateObject = new ExpandoObject() as IDictionary<string, object>;
    rateObject["Id"] = rate.Id;
    rateObject["RateType"] = rate.RateType;
    rateObject["Units"] = rate.R6Item.Unit.Symbol;
    rateObject["Schedule"] = rate.R6Item.Schedule.Description;
    rateObject["R6Code"] = rate.R6Item.Code;
    rateObject["R6Description"] = rate.R6Item.Description;
    rateObject["R7Code"] = rate.R7Item.Code;
    rateObject["R7Description"] = rate.R7Item.Description;
    rateObject["DICODE"] = rate.R6Item.Schedule.Discipline.Code;

    foreach (var currency in rate.Project.ProjectCurrencies)
    {
        rateObject[currency.Currency.Name] = currency.Currency.Name;
    }

    example.Add(rateObject);
}

Now when I expand the example object it looks like so:

enter image description here

and if I expand further it's like so:

enter image description here

Now how I want the data is how it's displayed inside the Dynamic View, is there a way to be able to access this? and have example just equal to the Dynamic View of the ExpandoObject?

1

There are 1 answers

0
Sinatr On BEST ANSWER

You can surely do a lot to enhance your debugging experience. Here is my quick attempt:

Preview

I had to use composition to extend sealed class ExpandoObject with attributes:

class MyExpando
{
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public ExpandoObject Value { get; } = new();

    [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
    public List<KeyValuePair<string, object?>> Properties => Value.ToList();

    public void Add(string key, object value) => ((IDictionary<string, object?>)Value)[key] = value;

    public override string ToString() => string.Join(", ", Value.Select(o => $"{o.Key}={o.Value}"));
}

There are:

  • overriden ToString() method used when displaying collections,
  • hidden in debugger property Value to access ExpandoObject,
  • property Properties used to visualize in debugger KeyValuePair entries.