From 5445592f20e5e645dce0df077afe7ecc2617b95f Mon Sep 17 00:00:00 2001 From: sr55 Date: Tue, 5 Jun 2018 19:32:36 +0100 Subject: WinGui: Additional Error Handling in EnumHelper --- win/CS/HandBrakeWPF/Utilities/EnumHelper.cs | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'win/CS/HandBrakeWPF/Utilities') diff --git a/win/CS/HandBrakeWPF/Utilities/EnumHelper.cs b/win/CS/HandBrakeWPF/Utilities/EnumHelper.cs index c4ceaeb94..718533aab 100644 --- a/win/CS/HandBrakeWPF/Utilities/EnumHelper.cs +++ b/win/CS/HandBrakeWPF/Utilities/EnumHelper.cs @@ -44,10 +44,15 @@ namespace HandBrakeWPF.Utilities } FieldInfo fieldInfo = value.GetType().GetField(value.ToString()); - DescriptionAttribute[] attributes = + if (fieldInfo != null) + { + DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes( typeof(DescriptionAttribute), false); - return (attributes.Length > 0) ? attributes[0].Description : value.ToString(); + return (attributes.Length > 0) ? attributes[0].Description : value.ToString(); + } + + return string.Empty; } /// @@ -63,9 +68,14 @@ namespace HandBrakeWPF.Utilities } FieldInfo fieldInfo = value.GetType().GetField(value.ToString()); - DisplayName[] attributes = (DisplayName[])fieldInfo.GetCustomAttributes(typeof(DisplayName), false); + if (fieldInfo != null) + { + DisplayName[] attributes = (DisplayName[])fieldInfo.GetCustomAttributes(typeof(DisplayName), false); + + return (attributes.Length > 0) ? attributes[0].Name : value.ToString(); + } - return (attributes.Length > 0) ? attributes[0].Name : value.ToString(); + return string.Empty; } /// @@ -124,9 +134,13 @@ namespace HandBrakeWPF.Utilities } FieldInfo fieldInfo = value.GetType().GetField(value.ToString()); - ShortName[] attributes = (ShortName[])fieldInfo.GetCustomAttributes(typeof(ShortName), false); + if (fieldInfo != null) + { + ShortName[] attributes = (ShortName[])fieldInfo.GetCustomAttributes(typeof(ShortName), false); + return (attributes.Length > 0) ? attributes[0].Name : value.ToString(); + } - return (attributes.Length > 0) ? attributes[0].Name : value.ToString(); + return string.Empty; } /// @@ -153,7 +167,10 @@ namespace HandBrakeWPF.Utilities { var strings = new Collection(); foreach (T e in Enum.GetValues(enumType)) + { strings.Add(GetDisplay(e)); + } + return strings; } -- cgit v1.2.3