From 308008a5ea2457ad7e78477c584c03bf7caefdc7 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 1 Feb 2019 22:42:40 +0900 Subject: [PATCH] Remove Expression from JsonEnumValidator --- .../JsonSchemaValidator/JsonEnumValidator.cs | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonEnumValidator.cs b/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonEnumValidator.cs index 8c6325ae0..4cd4ec7d8 100644 --- a/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonEnumValidator.cs +++ b/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonEnumValidator.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Linq.Expressions; using System.Reflection; + namespace UniJSON { public static class JsonEnumValidator @@ -304,8 +304,8 @@ namespace UniJSON GenericSerializer.Serialize(this, f, c, o); } - static class GenericDeserializer - where T: IListTreeItem, IValue + static class GenericDeserializer + where T : IListTreeItem, IValue { delegate U Deserializer(ListTreeNode src); static Deserializer s_d; @@ -319,13 +319,11 @@ namespace UniJSON var mi = typeof(Enum).GetMethods(BindingFlags.Static | BindingFlags.Public).First( x => x.Name == "Parse" && x.GetParameters().Length == 3 ); - var type = Expression.Constant(typeof(U)); - var value = Expression.Parameter(typeof(string), "value"); - var ic = Expression.Constant(true); - var call = Expression.Call(mi, type, value, ic); - var lambda = Expression.Lambda(call, value); - var func = (Func)lambda.Compile(); - s_d = x => GenericCast.Cast(func(x.GetString())); + s_d = x => + { + var enumValue = mi.Invoke(null, new object[] { typeof(U), x.GetString(), true }); + return GenericCast.Cast(enumValue); + }; } else { @@ -336,7 +334,7 @@ namespace UniJSON } } - public void Deserialize(ListTreeNode src, ref U dst) + public void Deserialize(ListTreeNode src, ref U dst) where T : IListTreeItem, IValue { GenericDeserializer.Deserialize(src, ref dst); @@ -414,7 +412,7 @@ namespace UniJSON f.Serialize(GenericCast.Cast(o)); } - static class GenericDeserializer + static class GenericDeserializer where T : IListTreeItem, IValue { delegate U Deserializer(ListTreeNode src); @@ -426,17 +424,13 @@ namespace UniJSON if (s_d == null) { // enum from int - var value = Expression.Parameter(typeof(int), "value"); - var cast = Expression.Convert(value, typeof(U)); - var lambda = Expression.Lambda(cast, value); - var func = (Func)lambda.Compile(); - s_d = s => func(s.GetInt32()); + s_d = s => GenericCast.Cast(s.GetInt32()); } dst = s_d(src); } } - public void Deserialize(ListTreeNode src, ref U dst) + public void Deserialize(ListTreeNode src, ref U dst) where T : IListTreeItem, IValue { GenericDeserializer.Deserialize(src, ref dst);