From 7cb4f634e72950ef3f6b51e3a495bbb7ca7e19f4 Mon Sep 17 00:00:00 2001 From: Jon Hanna Date: Sat, 14 Jan 2017 20:47:55 +0000 Subject: [PATCH] Optimise BinaryConverter's reflection object creation. (#1177) Make the test for a type being S.D.L.Binary a single test as Binary is a sealed class and therefore there's no need to test bases. Make the stored ReflectionObject static, reducing size and repeated allocations if BinaryConverter is explicitly created by the user. --- Src/Newtonsoft.Json/Converters/BinaryConverter.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Src/Newtonsoft.Json/Converters/BinaryConverter.cs b/Src/Newtonsoft.Json/Converters/BinaryConverter.cs index 1ceb8b36..f0f73c46 100644 --- a/Src/Newtonsoft.Json/Converters/BinaryConverter.cs +++ b/Src/Newtonsoft.Json/Converters/BinaryConverter.cs @@ -40,7 +40,7 @@ namespace Newtonsoft.Json.Converters #if HAVE_LINQ private const string BinaryTypeName = "System.Data.Linq.Binary"; private const string BinaryToArrayName = "ToArray"; - private ReflectionObject _reflectionObject; + private static ReflectionObject _reflectionObject; #endif /// @@ -65,7 +65,7 @@ namespace Newtonsoft.Json.Converters private byte[] GetByteArray(object value) { #if HAVE_LINQ - if (value.GetType().AssignableToTypeName(BinaryTypeName)) + if (value.GetType().FullName == BinaryTypeName) { EnsureReflectionObject(value.GetType()); return (byte[])_reflectionObject.GetValue(value, BinaryToArrayName); @@ -80,7 +80,7 @@ namespace Newtonsoft.Json.Converters } #if HAVE_LINQ - private void EnsureReflectionObject(Type t) + private static void EnsureReflectionObject(Type t) { if (_reflectionObject == null) { @@ -132,7 +132,7 @@ namespace Newtonsoft.Json.Converters : objectType; #if HAVE_LINQ - if (t.AssignableToTypeName(BinaryTypeName)) + if (t.FullName == BinaryTypeName) { EnsureReflectionObject(t); @@ -182,7 +182,7 @@ namespace Newtonsoft.Json.Converters public override bool CanConvert(Type objectType) { #if HAVE_LINQ - if (objectType.AssignableToTypeName(BinaryTypeName)) + if (objectType.FullName == BinaryTypeName) { return true; }