Skip to content

Commit

Permalink
Fix null handling in EnumExtensions (followup to #575)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobitege committed Jan 13, 2025
1 parent 503bb27 commit b72b83e
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions ZUGFeRD/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal static T IntToEnum<T>(this int value) where T : Enum
}
else
{
return default(T);
return default;
}
} // !IntToEnum()

Expand All @@ -53,7 +53,7 @@ internal static T StringToEnum<T>(this string value) where T : Enum
}
catch
{
return default(T);
return default;
}
} // !IntToEnum()

Expand All @@ -67,25 +67,30 @@ internal static int EnumToInt<T>(this T value) where T : Enum
internal static string GetDescriptionAttribute<T>(this T value) where T : Enum
{
FieldInfo field = value.GetType().GetField(value.ToString());
if (field == null)
{
return null;
}
DescriptionAttribute attribute = field.GetCustomAttribute<DescriptionAttribute>();
return attribute?.Description ?? value.ToString();
return attribute?.Description;
} // !GetDescriptionAttribute()


internal static T FromDescription<T>(string code) where T : Enum
{
if (string.IsNullOrEmpty(code))
{
return default(T);
return default;
}
foreach (T value in Enum.GetValues(typeof(T)))
{
if (value.GetDescriptionAttribute().Equals(code, StringComparison.OrdinalIgnoreCase))
var description = value.GetDescriptionAttribute();
if (description != null && description.Equals(code, StringComparison.OrdinalIgnoreCase))
{
return value;
}
}
return default(T);
return default;
} // !FromDescription()
}
}

0 comments on commit b72b83e

Please sign in to comment.