I'm attempting to create a 'Has Been Edited' tracker for my component classes, like this:
public class MyComponent : ObservableObject
{
// If any changes are made to properties that have the InspectableAttribute, this becomes true.
public bool HasBeenEdited
{
get => _hasBeenEdited;
protected set
{
if (value == _hasBeenEdited)
{
return;
}
_hasBeenEdited = value;
}
}
private bool _hasBeenEdited = false;
[Inspectable]
public string StringProperty
{
get => _stringProperty;
set => SetProperty(ref _stringProperty, value);
}
private string _stringProperty = null;
protected override void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyInfo propertyInfo = GetType().GetProperty(e.PropertyName);
if (Attribute.IsDefined(propertyInfo, typeof(InspectableAttribute)))
{
HasBeenEdited = true;
}
base.OnPropertyChanged(e);
}
}
However, now that I'm testing it, when my 'MyComponent' instances are being deserialised from Json, I get the following unhelpful exception:
Newtonsoft.Json.JsonSerializationException: Error setting value to 'StringProperty' on 'MyComponent'. Inner Exception: ArgumentNullException: Value cannot be null. Arg_ParamName_Name This exception was originally thrown at this call stack: System.Attribute.IsDefined(System.Reflection.MemberInfo, System.Type, bool) System.Attribute.IsDefined(System.Reflection.MemberInfo, System.Type) MyComponent.OnPropertyChanged(System.ComponentModel.PropertyChangedEventArgs) in MyComponent.cs ...
First of all, the error is obviously a bit typo'd. I'm not seeing a property to 'StringProperty'. I'm setting a property named StringProperty. But I doubt that's a real problem.
Second, this value can safely be set to null, and is in fact initialised as null.
When I remove the call to Attribute.IsDefined
the issue stops, suggesting that's somehow the problem, but it's not clear how.