Skip to content

Commit

Permalink
added [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Publ…
Browse files Browse the repository at this point in the history
…icFields)] annotations to generic enums
  • Loading branch information
filipw authored and abergs committed Oct 10, 2022
1 parent 1eda2a3 commit 753f48a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Src/Fido2.Models/Converters/EnumNameMapper.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.Serialization;

namespace Fido2NetLib
{
public static class EnumNameMapper<TEnum>
public static class EnumNameMapper<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TEnum>
where TEnum: struct, Enum
{
private static readonly Dictionary<TEnum, string> valueToNames = GetIdToNameMap();
Expand Down
3 changes: 2 additions & 1 deletion Src/Fido2.Models/Converters/FidoEnumConverter.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Fido2NetLib
{

public sealed class FidoEnumConverter<T> : JsonConverter<T>
public sealed class FidoEnumConverter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T> : JsonConverter<T>
where T: struct, Enum
{
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Expand Down
5 changes: 3 additions & 2 deletions Src/Fido2/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;

namespace Fido2NetLib;

Expand All @@ -12,7 +13,7 @@ public static class EnumExtensions
/// <param name="ignoreCase">ignores the case when comparing values.</param>
/// <returns>TEnum.</returns>
/// <exception cref="ArgumentException">No XmlEnumAttribute code exists for type " + typeof(TEnum).ToString() + " corresponding to value of " + value</exception>
public static TEnum ToEnum<TEnum>(this string value, bool ignoreCase = true) where TEnum : struct, Enum
public static TEnum ToEnum<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TEnum>(this string value, bool ignoreCase = true) where TEnum : struct, Enum
{
// Try to parse it normally on the first try
if (Enum.TryParse<TEnum>(value, ignoreCase, out var result))
Expand All @@ -33,7 +34,7 @@ public static TEnum ToEnum<TEnum>(this string value, bool ignoreCase = true) whe
/// <typeparam name="TEnum">The type of enum.</typeparam>
/// <param name="value">The enum's value.</param>
/// <returns>string.</returns>
public static string ToEnumMemberValue<TEnum>(this TEnum value) where TEnum : struct, Enum
public static string ToEnumMemberValue<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TEnum>(this TEnum value) where TEnum : struct, Enum
{
return EnumNameMapper<TEnum>.GetName(value);
}
Expand Down

0 comments on commit 753f48a

Please sign in to comment.