访问 ExpandoObject 中的动态视图

问题描述 投票:0回答:1

我有以下方法来创建这个

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);
}

现在,当我展开示例对象时,它看起来像这样:

如果我进一步扩展,就像这样:

现在我想要的数据是如何在动态视图中显示的,有没有办法能够访问它?并有与

ExpandoObject
的动态视图相同的示例?

c# expandoobject
1个回答
1
投票

您肯定可以做很多事情来增强您的调试体验。这是我的快速尝试:

我必须使用组合来扩展密封类

ExpandoObject
,其属性为:

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}"));
}

有:

  • 覆盖显示集合时使用的
    ToString()
    方法,
  • 隐藏在调试器属性
    Value
    中以访问
    ExpandoObject
    ,
  • 属性
    Properties
    用于在调试器
    KeyValuePair
    条目中可视化。
© www.soinside.com 2019 - 2024. All rights reserved.