Skip to content

Commit

Permalink
use formatter cache for TypelessObjectResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Oct 22, 2020
1 parent dddacef commit ccc02ab
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,35 @@ private TypelessObjectResolver()
/// <inheritdoc />
public IMessagePackFormatter<T> GetFormatter<T>()
{
if (typeof(T).IsAbstract || typeof(T).IsInterface)
{
return new ForceTypelessFormatter<T>();
}
return Cache<T>.Formatter;
}

if (typeof(T) == typeof(object))
{
return (IMessagePackFormatter<T>)TypelessFormatter.Instance;
}
else
private static class Cache<T>
{
public static readonly IMessagePackFormatter<T> Formatter;

static Cache()
{
foreach (IFormatterResolver item in Resolvers)
if (typeof(T).IsAbstract || typeof(T).IsInterface)
{
Formatter = new ForceTypelessFormatter<T>();
}

if (typeof(T) == typeof(object))
{
IMessagePackFormatter<T> f = item.GetFormatter<T>();
if (f != null)
Formatter = (IMessagePackFormatter<T>)TypelessFormatter.Instance;
}
else
{
foreach (var item in Resolvers)
{
return f;
var f = item.GetFormatter<T>();
if (f != null)
{
Formatter = f;
}
}
}

return null;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ public void OmitAssemblyVersion()
[Fact]
public void SerializeInterface()
{
var foo = MessagePackSerializer.Typeless.DefaultOptions.Resolver.GetFormatter<TypelessAbstract>();

var v = new Holder() { T1 = new TypelessInterface { X = 999 }, T2 = new TypelessNonAbstract { X = 19, Y = 9999 } };
var bin = MessagePackSerializer.Typeless.Serialize(v);
var v2 = MessagePackSerializer.Typeless.Deserialize(bin).IsInstanceOf<Holder>();
Expand Down

0 comments on commit ccc02ab

Please sign in to comment.