Skip to content

Commit

Permalink
Improved enum support.
Browse files Browse the repository at this point in the history
  • Loading branch information
SvenGroot committed Oct 26, 2023
1 parent 8c21109 commit 02a64ef
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/Ookii.Jumbo/IO/DefaultValueWriter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Sven Groot (Ookii.org)
using System;
using System.Collections.Generic;
using System.IO;

namespace Ookii.Jumbo.IO
Expand Down Expand Up @@ -577,16 +578,13 @@ private class EnumWriter<T, TUnderlying> : IValueWriter<T>
where T : Enum
where TUnderlying : notnull
{
private readonly IValueWriter<TUnderlying> _underlyingWriter = ValueWriter<TUnderlying>.Writer!;

public T Read(BinaryReader reader)
=> (T)Enum.ToObject(typeof(T), _underlyingWriter.Read(reader));
=> (T)Enum.ToObject(typeof(T), ValueWriter<TUnderlying>.ReadValue(reader));

public void Write(T value, BinaryWriter writer)
=> _underlyingWriter.Write((TUnderlying)Convert.ChangeType(value, typeof(TUnderlying)), writer);
=> ValueWriter<TUnderlying>.WriteValue((TUnderlying)Convert.ChangeType(value, typeof(TUnderlying)), writer);
}


#endregion

public static object GetWriter(Type type)
Expand Down

0 comments on commit 02a64ef

Please sign in to comment.