Serialize a User Dictionary with custom User Data

Hey developers!

In my plugin I need to attach custom UserData to geometry and have it grouped in scenarios. Each scenario, can hold different UserData values.

I have a custom BuildingData class derived from UserData attached to Geometry which serializes properly.

       buildingObject.Geometry.UserData.Add(new BuildingData(BuildingType.Residential, 10));

        public BuildingData( BuildingType type, double height )
        {
            Type = type;
            Height = height;
        }

        protected override bool Write( BinaryArchiveWriter archive )
        {
            var dictionary = new Rhino.Collections.ArchivableDictionary(1, nameof(BuildingData));
            dictionary.Set(nameof(Type), Type.ToString());
            dictionary.Set(nameof(Height), Height);
            archive.WriteDictionary(dictionary);

            return !archive.WriteErrorOccured;
        }

To group the entries under scenarios, I was thinking of using a Dictionary. How can one create a UserDictionary with a custom class, though? I tried using an ArchivableDictionary like so but this doesn’t serialize properly.

    var s1 = new ArchivableDictionary(new BuildingData(BuildingType.Residential, 10));
    buildingObject.Geometry.UserDictionary.Set("1", s1);

Any ideas?

Restructuring my BuildingData class as an ArchivableDictionary rather than UserData works fine. This way I can have nested Dictionaries.

var v1 = new ArchivableDictionary();
v1.Set("type", "Commercial");
v1.Set("height", 2.5);
buildingObject.Geometry.UserDictionary.Set("1", v1);

Still, I’d be interested in knowing whether there is a way of saving a UserData object to a UserDictionary.