Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GetLocalisableDescription() crashing on unnamed enum values #5335

Merged
merged 1 commit into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class LocalisableDescriptionAttributeTest
{
[TestCase(EnumA.Item1, "Item1")]
[TestCase(EnumA.Item2, "B")]
[TestCase((EnumA)3, "3")]
public void TestNonLocalisableDescriptionReturnsDescriptionOrToString(EnumA value, string expected)
{
Assert.That(value.GetLocalisableDescription().ToString(), Is.EqualTo(expected));
Expand Down
12 changes: 11 additions & 1 deletion osu.Framework/Extensions/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -199,7 +200,16 @@ public static LocalisableString GetLocalisableDescription<T>(this T value)
MemberInfo type;

if (value is Enum)
type = value.GetType().GetField(value.ToString());
{
string stringValue = value.ToString();
Debug.Assert(stringValue != null, "Enum.ToString() should not return null.");

type = value.GetType().GetField(stringValue);

// The enumeration may not contain the value, in which case just return the value as a string.
if (type == null)
return stringValue;
}
else
type = value as Type ?? value.GetType();

Expand Down