How to correctly implement a CollectionEditor?

41 Views Asked by At

I'm creating a class as shown in the code below. An object of this class is, eventually, shown in a PropertyGrid where, once selected, shows the ... button that opens the collection editor form.

[Editor(typeof(MyListBase<>.ItemsEditor), typeof(CollectionEditor))]
internal abstract class MyListBase<T> :  ICustomTypeDescriptor, IList<T> where T : IMyInterface
{
    /*
     * Class implementation
     */

    public class ItemsEditor : CollectionEditor
    {
        public ItemsEditor() : base(typeof(MyListBase<T>)) { }

        protected override object CreateInstance(Type itemType)
        {
            var ctor = itemType.GetConstructor(new Type[] { typeof(IMyOtherInterface) });
            if (ctor is not null)
                return ctor.Invoke(new object[] { this.Context.Container });

            return base.CreateInstance(itemType);
        }
    }

}

So far, so good.

The problem is that, when pressing the Add button the collection editor form displays an error saying that no constructor was found (as the image below).

Collection Form error

This lead me to believe, later confirmed by breakpoints, that the CreateInstance code is not being called.

Further investigation shown that the ItemsEditor class is not even being created.

IF I change the line:

[Editor(typeof(MyListBase<>.ItemsEditor), typeof(CollectionEditor))]

to:

[Editor(typeof(MyListBase<>.ItemsEditor), typeof(UITypeEditor))]

The ... button is not shown nor is the collection content in the PropertyGrid.

What am I doing wrong?

0

There are 0 best solutions below