bodong1987 / Avalonia.PropertyGrid

A property edit control in Avalonia like DevExpress's PropertyGridControl.
https://www.cnblogs.com/bodong
MIT License
187 stars 18 forks source link

FieldInfo calculation doesn't handle null #43

Closed LeftofZen closed 3 weeks ago

LeftofZen commented 1 month ago

This code here: https://github.com/bodong1987/Avalonia.PropertyGrid/blob/5753f10423dba995c37c0ca940971b2130e84a51/PropertyModels/ComponentModel/EnumDisplayNameAttribute.cs#L56

assumes that GetField will always return a non-null value. This is not always the case, for example in an enum that does not have a value of 0 in the enum definition, or in general when value has a value that is not defined in the enum.

I believe in this case, setting DisplayName to value.ToString(), similar to line 60, would be appropriate here.

bodong1987 commented 3 weeks ago

thanks .

LeftofZen commented 3 weeks ago

You didn't make any changes though?

bodong1987 commented 3 weeks ago

You didn't make any changes though?

In the next few days, I will submit a new version, which will enable nullable support, and I will do some special unit tests for the issue you raised. Please be patient and wait for a few days.

bodong1987 commented 3 weeks ago

You didn't make any changes though?

public EnumValueWrapper(Enum value)
{
    Debug.Assert(value != null);

    Value = value;

    var fieldInfo = value.GetType().GetField(value.ToString());

    var attr = fieldInfo?.GetCustomAttribute<EnumDisplayNameAttribute>();

    DisplayName = attr?.DisplayName ?? value.ToString();
}