From 8da911106651d243cb643c8acfefbe9141127ba4 Mon Sep 17 00:00:00 2001 From: Curtis Wensley Date: Wed, 5 Jul 2023 11:56:33 -0700 Subject: [PATCH] Re-throw property binding exceptions --- src/Eto/Forms/Binding/PropertyBinding.cs | 30 ++++++++++++--- .../Forms/Binding/PropertyBindingException.cs | 37 +++++++++++++++++++ 2 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 src/Eto/Forms/Binding/PropertyBindingException.cs diff --git a/src/Eto/Forms/Binding/PropertyBinding.cs b/src/Eto/Forms/Binding/PropertyBinding.cs index 069164940..29ae2ddbb 100644 --- a/src/Eto/Forms/Binding/PropertyBinding.cs +++ b/src/Eto/Forms/Binding/PropertyBinding.cs @@ -128,7 +128,20 @@ protected override T InternalGetValue(object dataItem) if (EnsureProperty(dataItem) && CanRead) { var propertyType = typeof(T); - object val = descriptor != null ? descriptor.GetValue(dataItem) : propInfo.GetValue(dataItem); + object val; + try + { + if (descriptor != null) + val = descriptor.GetValue(dataItem); + else if (propInfo != null) + val = propInfo.GetValue(dataItem); + else + return default(T); + } + catch (Exception ex) + { + throw new PropertyBindingException($"Could not get property '{Property}' on '{dataItem?.GetType()}'", ex); + } if (val != null && !propertyType.IsInstanceOfType(val)) { try @@ -171,10 +184,17 @@ protected override void InternalSetValue(object dataItem, T value) val = propertyType.GetTypeInfo().IsValueType ? Activator.CreateInstance(propertyType) : null; } } - if (descriptor != null) - descriptor.SetValue(dataItem, val); - else if (propInfo != null) - propInfo.SetValue(dataItem, val); + try + { + if (descriptor != null) + descriptor.SetValue(dataItem, val); + else if (propInfo != null) + propInfo.SetValue(dataItem, val); + } + catch (Exception ex) + { + throw new PropertyBindingException($"Could not set property '{Property}' on '{dataItem?.GetType()}'", ex); + } } } diff --git a/src/Eto/Forms/Binding/PropertyBindingException.cs b/src/Eto/Forms/Binding/PropertyBindingException.cs new file mode 100644 index 000000000..49dfd17df --- /dev/null +++ b/src/Eto/Forms/Binding/PropertyBindingException.cs @@ -0,0 +1,37 @@ +namespace Eto.Forms; + +/// +/// Exception when getting/setting values in a +/// +/// +/// This exception is thrown explicitly if there is a problem getting or setting the value on the data item. +/// Since using descriptors can sometimes bury the actual stack trace, this can be useful to figure out what property +/// setter/getter is throwing. +/// +[System.Serializable] +public class PropertyBindingException : System.Exception +{ + /// + /// Initializes a new instance of the PropertyBindingException class. + /// + public PropertyBindingException() { } + /// + /// Initializes a new instance of the PropertyBindingException class with the specified message. + /// + /// Message of the exception + public PropertyBindingException(string message) : base(message) { } + /// + /// Initializes a new instance of the PropertyBindingException class with the specified message and inner exception. + /// + /// Message of the exception + /// Original exception + public PropertyBindingException(string message, System.Exception inner) : base(message, inner) { } + /// + /// Initializes a new instance of the PropertyBindingException class from serialization. + /// + /// Serialization info + /// Streaming context + protected PropertyBindingException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } +}