HandyControl icon indicating copy to clipboard operation
HandyControl copied to clipboard

NullReferenceException when specifying unknown editor.

Open thargy opened this issue 3 years ago • 0 comments
trafficstars

Describe the bug

When specifying an unknown editor type on a Property we get a NullReferenceException when assigning to a PropertyGrid

Steps to reproduce the bug

Create a simple model with an invalid Editor type, e.g.:

public class MyModel
{
    [Category("General")]
    [DisplayName("Delay")]
    [Description("An example editor.")]
    [DefaultValue(typeof(TimeSpan), "00:00:03")]
    [Editor("UnknownType, MyAssembly", (string?)null)]
    public TimeSpan Delay
    {
        get => GetSetting<TimeSpan>();
        set => SetSetting(value);
    }
}

and assign to a PropertyGrid.SelectedObject.

Expected behavior

The PropertyGrid should default to a ReadOnlyTextPropertyEditor.

Screenshots

No response

NuGet package version

HandyControl 3.4.0

IDE

Visual Studio 2022

Framework type

.Net 6.0

Windows version

Windows 11 (22000)

Additional context

The error is thrown here: https://github.com/HandyOrg/HandyControl/blob/465433560f0fe96bf2bc71e93f60b923d21c7bc6/src/Shared/HandyControl_Shared/Controls/PropertyGrid/PropertyResolver.cs#L105

As type can be null if the editorAttribute.EditorTypeName type cannot be found here: https://github.com/HandyOrg/HandyControl/blob/465433560f0fe96bf2bc71e93f60b923d21c7bc6/src/Shared/HandyControl_Shared/Controls/PropertyGrid/PropertyResolver.cs#L72-L74

I recommend changing CreateEditor to the following to get the desired behaviour:

    public virtual PropertyEditorBase CreateEditor(Type? type)
        => (type is null ? null : Activator.CreateInstance(type) as PropertyEditorBase) ?? new ReadOnlyTextPropertyEditor();

thargy avatar Oct 27 '22 00:10 thargy