HandyOrg / HandyOrg/HandyControl
NullReferenceException when specifying unknown editor.
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 7.2k
- Forks
- 1.2k
- PR merge metrics
- No merged PRs in 30d
Description
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();
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/Shared/HandyControl_Shared/Controls/PropertyGrid/PropertyResolver.cs, especially CreateEditor and the editor type lookup around lines 72-74 and 105. Reproduce the issue with the invalid Editor attribute shown in the report, then verify that assigning the model to PropertyGrid.SelectedObject no longer throws and uses the expected read-only text editor.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100