Skip to content

Commit

Permalink
Handle nested enums in constructed generic types
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed Apr 13, 2018
1 parent ee6129f commit 0720981
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
10 changes: 10 additions & 0 deletions src/ZeroLog.Tests/EnumCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ public void should_not_throw_when_registering_enum_in_open_type()
EnumCache.Register(typeof(GenericType<>.AnotherOne<>.EnumInGenericType2));
}

[Test]
public void should_handle_nested_enums_in_constructed_generic_types()
{
Check.That(GetString(GenericType<int>.EnumInGenericType.Foo)).IsEqualTo("Foo");
Check.That(GetString(GenericType<int>.AnotherOne<string>.EnumInGenericType2.Foo)).IsEqualTo("Foo");
}

private static string GetString<T>(T value)
where T : struct
{
Expand Down Expand Up @@ -335,16 +342,19 @@ private enum EnumMinMaxUInt64 : ulong
}

[SuppressMessage("ReSharper", "ClassNeverInstantiated.Local")]
[SuppressMessage("ReSharper", "UnusedTypeParameter")]
private class GenericType<TFoo>
{
public enum EnumInGenericType
{
Foo = 42
}

public class AnotherOne<TBar>
{
public enum EnumInGenericType2
{
Foo = 42
}
}
}
Expand Down
14 changes: 2 additions & 12 deletions src/ZeroLog/EnumCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,8 @@ public static void Register([NotNull] Type enumType)
if (!enumType.IsEnum)
throw new ArgumentException($"Not an enum type: {enumType}");

if (enumType.IsNested)
{
var declaringType = enumType.DeclaringType;

while (declaringType != null)
{
if (declaringType.IsGenericType)
return;

declaringType = declaringType.DeclaringType;
}
}
if (enumType.ContainsGenericParameters)
return;

_enums.TryAdd(TypeUtil.GetTypeHandleSlow(enumType), EnumStrings.Create(enumType));
}
Expand Down

0 comments on commit 0720981

Please sign in to comment.