Skip to content

Replies

0
Jared Lessl
Jared Lessl answered on May 15, 2019 3:02 PM

I’m trying to take an inputted list of JSON data structures and display the contents in the grid.  Which means I will know absolutely nothing about the structure beforehand.

> the columns of every row in the band have to be the same

So if I have data structured like this:

  • A1
    • B1
      • C1
    • B2
      • C2
      • C3
  • A2
    • B3
      • C4

You’re saying that while the A’s, B’s, and C’s can differ from each other, they would need to be consistent within themselves?  Shouldn’t be a problem.  In theory, there’s no enforcing that sort of compliance, but in practice they’ll pretty much be the same.  This is for a debugging tool anyway.

> it seems to be that you are essentially returning different PropertyDescriptors from GetProperties at different times for the same type

Correct.  Which is unavoidable, because it’s just a generic dictionary type.


But anyway, I discovered that returning the PropertyDescriptors in the dictionary object itself (via ICustomTypeDescriptor) rather than from the containing list worked.  Just required a custom JsonConverter in the mix to deserialize all objects to PropertyBags.

 

Here’s the gist of it.

class PropertyBag : Dictionary<string, object>, ICustomTypeDescriptor
{
    public PropertyDescriptorCollection GetProperties()
    {
      return new PropertyDescriptorCollection(this.Select(kvp => new CustomPropertyDescriptor(kvp.Key, kvp.Value?.GetType())).ToArray());
    }
}

class CustomPropertyDescriptor : PropertyDescriptor
{
    private string _Name;
    private Type _Type;

    public CustomPropertyDescriptor(string name, Type type): base(name, new Attribute[0])
    {
        _Name = name;
        _Type = type;
    }

    public override object GetValue(object component)
    {
        var bag = (PropertyBag)component;
        if (bag != null) return bag[_Name];
        return null;
    }
}

 

 

0
Jared Lessl
Jared Lessl answered on May 15, 2019 11:54 AM

Used a custom Dictionary<string, object> storage class that implements ICustomTypeDescriptor, that seemed to work.