From 49b57e7ef3c3be9c048f22a24550227af4aa2855 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Sat, 2 Feb 2019 16:39:03 +0900 Subject: [PATCH 1/7] GenericInvokeCallFactory.Create to CreateAction --- .../Editor/Tests/GenericCallUtilityTests.cs | 4 ++-- .../Scripts/FormatterExtensionsSerializer.cs | 10 +++++----- .../GenericInvokeCallFactory.cs | 20 +++++++++++++------ .../GenericInvokeCallFactory.g.cs | 16 +++++++-------- .../JsonSchemaValidator/JsonArrayValidator.cs | 2 +- 5 files changed, 30 insertions(+), 22 deletions(-) diff --git a/Assets/VRM/UniJSON/Editor/Tests/GenericCallUtilityTests.cs b/Assets/VRM/UniJSON/Editor/Tests/GenericCallUtilityTests.cs index 9a6f73b22..793ce922d 100644 --- a/Assets/VRM/UniJSON/Editor/Tests/GenericCallUtilityTests.cs +++ b/Assets/VRM/UniJSON/Editor/Tests/GenericCallUtilityTests.cs @@ -31,11 +31,11 @@ namespace UniJSON var mi = s.GetType().GetMethod("Set"); - var invoke = (Action)GenericInvokeCallFactory.Create(mi); + var invoke = GenericInvokeCallFactory.CreateAction(mi); invoke(s, 1); Assert.AreEqual(1, s.Value); - var exp = (Action)GenericExpressionCallFactory.Create(mi); + var exp = GenericExpressionCallFactory.Create(mi); exp(s, 2); Assert.AreEqual(2, s.Value); } diff --git a/Assets/VRM/UniJSON/Scripts/FormatterExtensionsSerializer.cs b/Assets/VRM/UniJSON/Scripts/FormatterExtensionsSerializer.cs index ca5b873c8..16350f3fc 100644 --- a/Assets/VRM/UniJSON/Scripts/FormatterExtensionsSerializer.cs +++ b/Assets/VRM/UniJSON/Scripts/FormatterExtensionsSerializer.cs @@ -84,7 +84,7 @@ namespace UniJSON if (typeof(T) == typeof(object) && t.GetType() != typeof(object)) { var mi = FormatterExtensionsSerializer.GetMethod("SerializeObject"); - return GenericInvokeCallFactory.Create(mi); + return GenericInvokeCallFactory.CreateAction(mi); } try @@ -93,7 +93,7 @@ namespace UniJSON var mi = typeof(IFormatter).GetMethod("Value", new Type[] { t }); if (mi != null) { - return GenericInvokeCallFactory.Create(mi); + return GenericInvokeCallFactory.CreateAction(mi); } } catch (AmbiguousMatchException) @@ -111,7 +111,7 @@ namespace UniJSON if (idictionary != null) { var mi = FormatterExtensionsSerializer.GetMethod("SerializeDictionary"); - return GenericInvokeCallFactory.Create(mi); + return GenericInvokeCallFactory.CreateAction(mi); } } @@ -120,7 +120,7 @@ namespace UniJSON if (t == typeof(object[])) { var mi = FormatterExtensionsSerializer.GetMethod("SerializeObjectArray"); - return GenericInvokeCallFactory.Create(mi); + return GenericInvokeCallFactory.CreateAction(mi); } } @@ -134,7 +134,7 @@ namespace UniJSON { var g = FormatterExtensionsSerializer.GetMethod("SerializeArray"); var mi = g.MakeGenericMethod(ienumerable.GetGenericArguments()); - return GenericInvokeCallFactory.Create(mi); + return GenericInvokeCallFactory.CreateAction(mi); } } diff --git a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs index 76cd2528c..01c968fe7 100644 --- a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs +++ b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs @@ -43,7 +43,7 @@ namespace UniJSON "); // CreateWithThis - w.WriteLine("//////////// Create"); + w.WriteLine("//////////// CreateAction"); // Create for (int i = 1; i <= ARGS && i< NET35MAX; ++i) @@ -54,7 +54,7 @@ namespace UniJSON var source = @" - public static Action Create(MethodInfo m) + public static Action CreateAction(MethodInfo m) { Action callback = null; if (m.IsStatic) @@ -88,7 +88,7 @@ namespace UniJSON var a = String.Join(", ", GetArgs("a", i).ToArray()); var source = @" - public static Action<$0> CreateWithThis(MethodInfo m, S instance) + public static Action<$0> CreateActionBindThis(MethodInfo m, S instance) { if (m.IsStatic) { @@ -130,8 +130,8 @@ namespace UniJSON } #endif - #region no arguments - public static Action Create(MethodInfo m) + #region zero arguments + public static Action CreateAction(MethodInfo m) { return (s) => { @@ -139,7 +139,15 @@ namespace UniJSON }; } - public static Action CreateWithThis(MethodInfo m, S instance) + public static Func CreateFunc(MethodInfo m) + { + return (s) => + { + return (T)m.Invoke(s, new object[] { }); + }; + } + + public static Action CreateActionBindThis(MethodInfo m, S instance) { return () => { diff --git a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs index ce26ca6d5..6b55983c2 100644 --- a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs +++ b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs @@ -8,9 +8,9 @@ namespace UniJSON public static partial class GenericInvokeCallFactory { -//////////// Create +//////////// CreateAction - public static Action Create(MethodInfo m) + public static Action CreateAction(MethodInfo m) { Action callback = null; if (m.IsStatic) @@ -31,7 +31,7 @@ namespace UniJSON } - public static Action Create(MethodInfo m) + public static Action CreateAction(MethodInfo m) { Action callback = null; if (m.IsStatic) @@ -52,7 +52,7 @@ namespace UniJSON } - public static Action Create(MethodInfo m) + public static Action CreateAction(MethodInfo m) { Action callback = null; if (m.IsStatic) @@ -74,7 +74,7 @@ namespace UniJSON //////////// CreateWithThis - public static Action CreateWithThis(MethodInfo m, S instance) + public static Action CreateActionBindThis(MethodInfo m, S instance) { if (m.IsStatic) { @@ -100,7 +100,7 @@ namespace UniJSON } - public static Action CreateWithThis(MethodInfo m, S instance) + public static Action CreateActionBindThis(MethodInfo m, S instance) { if (m.IsStatic) { @@ -126,7 +126,7 @@ namespace UniJSON } - public static Action CreateWithThis(MethodInfo m, S instance) + public static Action CreateActionBindThis(MethodInfo m, S instance) { if (m.IsStatic) { @@ -152,7 +152,7 @@ namespace UniJSON } - public static Action CreateWithThis(MethodInfo m, S instance) + public static Action CreateActionBindThis(MethodInfo m, S instance) { if (m.IsStatic) { diff --git a/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonArrayValidator.cs b/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonArrayValidator.cs index 2859ebd2b..492c33031 100644 --- a/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonArrayValidator.cs +++ b/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonArrayValidator.cs @@ -264,7 +264,7 @@ namespace UniJSON IFormatter, JsonSchemaValidationContext, T>) - GenericInvokeCallFactory.Create< + GenericInvokeCallFactory.CreateAction< IJsonSchemaValidator, IFormatter, JsonSchemaValidationContext, From da562a18c02e4c73eb53edfa694c806d898a6c64 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Sat, 2 Feb 2019 17:27:28 +0900 Subject: [PATCH 2/7] Refactor GenericInvokeCallFactory. StaticAction/Func, OpenAction/Func, BindAction/Func --- .../Editor/Tests/GenericCallUtilityTests.cs | 31 +- .../Scripts/FormatterExtensionsSerializer.cs | 10 +- .../GenericInvokeCallFactory.cs | 267 ++++++++---- .../GenericInvokeCallFactory.g.cs | 384 +++++++++++------- .../JsonSchemaValidator/JsonArrayValidator.cs | 2 +- 5 files changed, 475 insertions(+), 219 deletions(-) diff --git a/Assets/VRM/UniJSON/Editor/Tests/GenericCallUtilityTests.cs b/Assets/VRM/UniJSON/Editor/Tests/GenericCallUtilityTests.cs index 793ce922d..ee51bbc8c 100644 --- a/Assets/VRM/UniJSON/Editor/Tests/GenericCallUtilityTests.cs +++ b/Assets/VRM/UniJSON/Editor/Tests/GenericCallUtilityTests.cs @@ -20,6 +20,11 @@ namespace UniJSON { Value = value; } + + public int Get(int _) + { + return Value; + } } @@ -29,15 +34,27 @@ namespace UniJSON { var s = new Sample(); - var mi = s.GetType().GetMethod("Set"); - var invoke = GenericInvokeCallFactory.CreateAction(mi); - invoke(s, 1); - Assert.AreEqual(1, s.Value); + { + var mi = s.GetType().GetMethod("Set"); + var action = GenericInvokeCallFactory.OpenAction(mi); + action(s, 1); + Assert.AreEqual(1, s.Value); + } - var exp = GenericExpressionCallFactory.Create(mi); - exp(s, 2); - Assert.AreEqual(2, s.Value); + { + var mi = s.GetType().GetMethod("Get"); + var func = GenericInvokeCallFactory.OpenFunc(mi); + var value = func(s, 1); + Assert.AreEqual(1, s.Value); + } + + { + var mi = s.GetType().GetMethod("Set"); + var action = GenericExpressionCallFactory.Create(mi); + action(s, 2); + Assert.AreEqual(2, s.Value); + } } } } diff --git a/Assets/VRM/UniJSON/Scripts/FormatterExtensionsSerializer.cs b/Assets/VRM/UniJSON/Scripts/FormatterExtensionsSerializer.cs index 16350f3fc..473dee4d3 100644 --- a/Assets/VRM/UniJSON/Scripts/FormatterExtensionsSerializer.cs +++ b/Assets/VRM/UniJSON/Scripts/FormatterExtensionsSerializer.cs @@ -84,7 +84,7 @@ namespace UniJSON if (typeof(T) == typeof(object) && t.GetType() != typeof(object)) { var mi = FormatterExtensionsSerializer.GetMethod("SerializeObject"); - return GenericInvokeCallFactory.CreateAction(mi); + return GenericInvokeCallFactory.StaticAction(mi); } try @@ -93,7 +93,7 @@ namespace UniJSON var mi = typeof(IFormatter).GetMethod("Value", new Type[] { t }); if (mi != null) { - return GenericInvokeCallFactory.CreateAction(mi); + return GenericInvokeCallFactory.OpenAction(mi); } } catch (AmbiguousMatchException) @@ -111,7 +111,7 @@ namespace UniJSON if (idictionary != null) { var mi = FormatterExtensionsSerializer.GetMethod("SerializeDictionary"); - return GenericInvokeCallFactory.CreateAction(mi); + return GenericInvokeCallFactory.StaticAction(mi); } } @@ -120,7 +120,7 @@ namespace UniJSON if (t == typeof(object[])) { var mi = FormatterExtensionsSerializer.GetMethod("SerializeObjectArray"); - return GenericInvokeCallFactory.CreateAction(mi); + return GenericInvokeCallFactory.StaticAction(mi); } } @@ -134,7 +134,7 @@ namespace UniJSON { var g = FormatterExtensionsSerializer.GetMethod("SerializeArray"); var mi = g.MakeGenericMethod(ienumerable.GetGenericArguments()); - return GenericInvokeCallFactory.CreateAction(mi); + return GenericInvokeCallFactory.StaticAction(mi); } } diff --git a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs index 01c968fe7..290cfb91c 100644 --- a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs +++ b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs @@ -1,15 +1,23 @@ using System; +using System.Reflection; +#if UNITY_EDITOR && VRM_DEVELOP using System.IO; using System.Linq; -using System.Reflection; using System.Text; -#if UNITY_EDITOR using UnityEditor; #endif namespace UniJSON { + /// + /// MethodInfoからDelegateを作成する + /// + /// * StaticAction/Func StaticMethod呼び出し + /// * OpenAction/Func 第1引数にthisを受けるメソッド呼び出し + /// * BindAction/Func thisを内部に保持したメソッド呼び出し + /// + /// public static partial class GenericInvokeCallFactory { #if UNITY_EDITOR && VRM_DEVELOP @@ -42,79 +50,151 @@ namespace UniJSON { "); - // CreateWithThis - w.WriteLine("//////////// CreateAction"); - - // Create - for (int i = 1; i <= ARGS && i< NET35MAX; ++i) - { - var g = String.Join(", ", GetArgs("A", i).ToArray()); - var a = String.Join(", ", GetArgs("a", i).ToArray()); - - - - var source = @" - public static Action CreateAction(MethodInfo m) - { - Action callback = null; - if (m.IsStatic) - { - callback = (s, $1) => - { - m.Invoke(null, new object[] { s, $1 }); - }; - } - else - { - callback = (s, $1) => - { - m.Invoke(s, new object[] { $1 }); - }; - } - return callback; - } -".Replace("$0", g).Replace("$1", a); - - w.WriteLine(source); - - } - - - // CreateWithThis - w.WriteLine("//////////// CreateWithThis"); - for (int i = 1; i <= ARGS && i<= NET35MAX; ++i) + // StaticAction + w.WriteLine("//////////// StaticAction"); + for (int i = 1; i <= ARGS && i <= NET35MAX; ++i) { var g = String.Join(", ", GetArgs("A", i).ToArray()); var a = String.Join(", ", GetArgs("a", i).ToArray()); var source = @" - public static Action<$0> CreateActionBindThis(MethodInfo m, S instance) + public static Action<$0> StaticAction<$0>(MethodInfo m) { - if (m.IsStatic) + if (!m.IsStatic) { - if (instance != null) - { - throw new ArgumentException(); - } - } - else - { - if (instance == null) - { - throw new ArgumentNullException(); - } + throw new ArgumentException(string.Format(""{0} is not static"", m)); } - // ToDo: CreateDelegate - Action<$0> callback= - ($1) => { - m.Invoke(instance, new object[]{ $1 }); + return ($1) => + { + m.Invoke(null, new object[] { $1 }); }; - return callback; } ".Replace("$0", g).Replace("$1", a); w.WriteLine(source); + + } + + // OpenAction + w.WriteLine("//////////// OpenAction"); + for (int i = 1; i <= ARGS && i < NET35MAX; ++i) + { + var g = String.Join(", ", GetArgs("A", i).ToArray()); + var a = String.Join(", ", GetArgs("a", i).ToArray()); + + var source = @" + public static Action OpenAction(MethodInfo m) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format(""{0} is static"", m)); + } + + return (s, $1) => + { + m.Invoke(s, new object[] { $1 }); + }; + } +".Replace("$0", g).Replace("$1", a); + + w.WriteLine(source); + + } + + // BindAction + w.WriteLine("//////////// BindAction"); + for (int i = 1; i <= ARGS && i <= NET35MAX; ++i) + { + var g = String.Join(", ", GetArgs("A", i).ToArray()); + var a = String.Join(", ", GetArgs("a", i).ToArray()); + + var source = @" + public static Action<$0> BindAction(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format(""{0} is static"", m)); + } + + return ($1) => + { + m.Invoke(instance, new object[] { $1 }); + }; + } +".Replace("$0", g).Replace("$1", a); + + w.WriteLine(source); + + } + + // StaticFunc + w.WriteLine("//////////// StaticFunc"); + for (int i = 1; i <= ARGS && i <= NET35MAX; ++i) + { + var g = String.Join(", ", GetArgs("A", i).ToArray()); + var a = String.Join(", ", GetArgs("a", i).ToArray()); + + var source = @" + public static Func<$0, T> StaticFunc<$0, T>(MethodInfo m) + { + if (!m.IsStatic) + { + throw new ArgumentException(string.Format(""{0} is not static"", m)); + } + + return ($1) => (T)m.Invoke(null, new object[] { $1 }); + } +".Replace("$0", g).Replace("$1", a); + + w.WriteLine(source); + + } + + // OpenFunc + w.WriteLine("//////////// OpenFunc"); + for (int i = 1; i <= ARGS && i < NET35MAX; ++i) + { + var g = String.Join(", ", GetArgs("A", i).ToArray()); + var a = String.Join(", ", GetArgs("a", i).ToArray()); + + var source = @" + public static Func OpenFunc(MethodInfo m) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format(""{0} is static"", m)); + } + + return (s, $1) => (T)m.Invoke(s, new object[] { $1 }); + } +".Replace("$0", g).Replace("$1", a); + + w.WriteLine(source); + + } + + // BindFunc + w.WriteLine("//////////// BindFunc"); + for (int i = 1; i <= ARGS && i <= NET35MAX; ++i) + { + var g = String.Join(", ", GetArgs("A", i).ToArray()); + var a = String.Join(", ", GetArgs("a", i).ToArray()); + + var source = @" + public static Func<$0, T> BindFunc(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format(""{0} is static"", m)); + } + + return ($1) => (T)m.Invoke(instance, new object[] { $1 }); + } +".Replace("$0", g).Replace("$1", a); + + w.WriteLine(source); + } @@ -130,30 +210,77 @@ namespace UniJSON } #endif - #region zero arguments - public static Action CreateAction(MethodInfo m) + #region Action without arguments + public static Action StaticAction(MethodInfo m) { + if (!m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is not static", m)); + } + + return () => + { + m.Invoke(null, new object[] { }); + }; + } + + public static Action OpenAction(MethodInfo m) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + return (s) => { m.Invoke(s, new object[] { }); }; } - public static Func CreateFunc(MethodInfo m) + public static Action BindAction(MethodInfo m, S instance) { - return (s) => + if (m.IsStatic) { - return (T)m.Invoke(s, new object[] { }); - }; - } + throw new ArgumentException(string.Format("{0} is static", m)); + } - public static Action CreateActionBindThis(MethodInfo m, S instance) - { return () => { m.Invoke(instance, new object[] { }); }; } #endregion + + #region Func without arguments + public static Func StaticFunc(MethodInfo m) + { + if (!m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is not static", m)); + } + + return () => (T)m.Invoke(null, new object[] { }); + } + + public static Func OpenFunc(MethodInfo m) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (s) => (T)m.Invoke(s, new object[] { }); + } + + public static Func BindFunc(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return () => (T)m.Invoke(instance, new object[] { }); + } + #endregion } } diff --git a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs index 6b55983c2..209b49be0 100644 --- a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs +++ b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs @@ -8,173 +8,285 @@ namespace UniJSON public static partial class GenericInvokeCallFactory { -//////////// CreateAction +//////////// StaticAction - public static Action CreateAction(MethodInfo m) + public static Action StaticAction(MethodInfo m) { - Action callback = null; - if (m.IsStatic) + if (!m.IsStatic) { - callback = (s, a0) => - { - m.Invoke(null, new object[] { s, a0 }); - }; - } - else - { - callback = (s, a0) => - { - m.Invoke(s, new object[] { a0 }); - }; - } - return callback; - } - - - public static Action CreateAction(MethodInfo m) - { - Action callback = null; - if (m.IsStatic) - { - callback = (s, a0, a1) => - { - m.Invoke(null, new object[] { s, a0, a1 }); - }; - } - else - { - callback = (s, a0, a1) => - { - m.Invoke(s, new object[] { a0, a1 }); - }; - } - return callback; - } - - - public static Action CreateAction(MethodInfo m) - { - Action callback = null; - if (m.IsStatic) - { - callback = (s, a0, a1, a2) => - { - m.Invoke(null, new object[] { s, a0, a1, a2 }); - }; - } - else - { - callback = (s, a0, a1, a2) => - { - m.Invoke(s, new object[] { a0, a1, a2 }); - }; - } - return callback; - } - -//////////// CreateWithThis - - public static Action CreateActionBindThis(MethodInfo m, S instance) - { - if (m.IsStatic) - { - if (instance != null) - { - throw new ArgumentException(); - } - } - else - { - if (instance == null) - { - throw new ArgumentNullException(); - } + throw new ArgumentException(string.Format("{0} is not static", m)); } - // ToDo: CreateDelegate - Action callback= - (a0) => { - m.Invoke(instance, new object[]{ a0 }); + return (a0) => + { + m.Invoke(null, new object[] { a0 }); }; - return callback; } - public static Action CreateActionBindThis(MethodInfo m, S instance) + public static Action StaticAction(MethodInfo m) { - if (m.IsStatic) + if (!m.IsStatic) { - if (instance != null) - { - throw new ArgumentException(); - } - } - else - { - if (instance == null) - { - throw new ArgumentNullException(); - } + throw new ArgumentException(string.Format("{0} is not static", m)); } - // ToDo: CreateDelegate - Action callback= - (a0, a1) => { - m.Invoke(instance, new object[]{ a0, a1 }); + return (a0, a1) => + { + m.Invoke(null, new object[] { a0, a1 }); }; - return callback; } - public static Action CreateActionBindThis(MethodInfo m, S instance) + public static Action StaticAction(MethodInfo m) { - if (m.IsStatic) + if (!m.IsStatic) { - if (instance != null) - { - throw new ArgumentException(); - } - } - else - { - if (instance == null) - { - throw new ArgumentNullException(); - } + throw new ArgumentException(string.Format("{0} is not static", m)); } - // ToDo: CreateDelegate - Action callback= - (a0, a1, a2) => { - m.Invoke(instance, new object[]{ a0, a1, a2 }); + return (a0, a1, a2) => + { + m.Invoke(null, new object[] { a0, a1, a2 }); }; - return callback; } - public static Action CreateActionBindThis(MethodInfo m, S instance) + public static Action StaticAction(MethodInfo m) + { + if (!m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is not static", m)); + } + + return (a0, a1, a2, a3) => + { + m.Invoke(null, new object[] { a0, a1, a2, a3 }); + }; + } + +//////////// OpenAction + + public static Action OpenAction(MethodInfo m) { if (m.IsStatic) { - if (instance != null) - { - throw new ArgumentException(); - } - } - else - { - if (instance == null) - { - throw new ArgumentNullException(); - } + throw new ArgumentException(string.Format("{0} is static", m)); } - // ToDo: CreateDelegate - Action callback= - (a0, a1, a2, a3) => { - m.Invoke(instance, new object[]{ a0, a1, a2, a3 }); + return (s, a0) => + { + m.Invoke(s, new object[] { a0 }); }; - return callback; + } + + + public static Action OpenAction(MethodInfo m) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (s, a0, a1) => + { + m.Invoke(s, new object[] { a0, a1 }); + }; + } + + + public static Action OpenAction(MethodInfo m) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (s, a0, a1, a2) => + { + m.Invoke(s, new object[] { a0, a1, a2 }); + }; + } + +//////////// BindAction + + public static Action BindAction(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (a0) => + { + m.Invoke(instance, new object[] { a0 }); + }; + } + + + public static Action BindAction(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (a0, a1) => + { + m.Invoke(instance, new object[] { a0, a1 }); + }; + } + + + public static Action BindAction(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (a0, a1, a2) => + { + m.Invoke(instance, new object[] { a0, a1, a2 }); + }; + } + + + public static Action BindAction(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (a0, a1, a2, a3) => + { + m.Invoke(instance, new object[] { a0, a1, a2, a3 }); + }; + } + +//////////// StaticFunc + + public static Func StaticFunc(MethodInfo m) + { + if (!m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is not static", m)); + } + + return (a0) => (T)m.Invoke(null, new object[] { a0 }); + } + + + public static Func StaticFunc(MethodInfo m) + { + if (!m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is not static", m)); + } + + return (a0, a1) => (T)m.Invoke(null, new object[] { a0, a1 }); + } + + + public static Func StaticFunc(MethodInfo m) + { + if (!m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is not static", m)); + } + + return (a0, a1, a2) => (T)m.Invoke(null, new object[] { a0, a1, a2 }); + } + + + public static Func StaticFunc(MethodInfo m) + { + if (!m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is not static", m)); + } + + return (a0, a1, a2, a3) => (T)m.Invoke(null, new object[] { a0, a1, a2, a3 }); + } + +//////////// OpenFunc + + public static Func OpenFunc(MethodInfo m) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (s, a0) => (T)m.Invoke(s, new object[] { a0 }); + } + + + public static Func OpenFunc(MethodInfo m) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (s, a0, a1) => (T)m.Invoke(s, new object[] { a0, a1 }); + } + + + public static Func OpenFunc(MethodInfo m) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (s, a0, a1, a2) => (T)m.Invoke(s, new object[] { a0, a1, a2 }); + } + +//////////// BindFunc + + public static Func BindFunc(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (a0) => (T)m.Invoke(instance, new object[] { a0 }); + } + + + public static Func BindFunc(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (a0, a1) => (T)m.Invoke(instance, new object[] { a0, a1 }); + } + + + public static Func BindFunc(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (a0, a1, a2) => (T)m.Invoke(instance, new object[] { a0, a1, a2 }); + } + + + public static Func BindFunc(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (a0, a1, a2, a3) => (T)m.Invoke(instance, new object[] { a0, a1, a2, a3 }); } diff --git a/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonArrayValidator.cs b/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonArrayValidator.cs index 492c33031..e9ea2c8eb 100644 --- a/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonArrayValidator.cs +++ b/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonArrayValidator.cs @@ -264,7 +264,7 @@ namespace UniJSON IFormatter, JsonSchemaValidationContext, T>) - GenericInvokeCallFactory.CreateAction< + GenericInvokeCallFactory.StaticAction< IJsonSchemaValidator, IFormatter, JsonSchemaValidationContext, From 7086512cb89fefc53e83d7a85c56536dfc4a583d Mon Sep 17 00:00:00 2001 From: ousttrue Date: Sat, 2 Feb 2019 17:40:30 +0900 Subject: [PATCH 3/7] Use Delegate.CreateDelegate --- .../GenericInvokeCallFactory.cs | 26 +++------ .../GenericInvokeCallFactory.g.cs | 55 ++++--------------- 2 files changed, 18 insertions(+), 63 deletions(-) diff --git a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs index 290cfb91c..4a543029f 100644 --- a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs +++ b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs @@ -65,10 +65,7 @@ namespace UniJSON throw new ArgumentException(string.Format(""{0} is not static"", m)); } - return ($1) => - { - m.Invoke(null, new object[] { $1 }); - }; + return (Action<$0>)Delegate.CreateDelegate(typeof(Action<$0>), null, m); } ".Replace("$0", g).Replace("$1", a); @@ -91,10 +88,7 @@ namespace UniJSON throw new ArgumentException(string.Format(""{0} is static"", m)); } - return (s, $1) => - { - m.Invoke(s, new object[] { $1 }); - }; + return (Action)Delegate.CreateDelegate(typeof(Action), m); } ".Replace("$0", g).Replace("$1", a); @@ -117,10 +111,7 @@ namespace UniJSON throw new ArgumentException(string.Format(""{0} is static"", m)); } - return ($1) => - { - m.Invoke(instance, new object[] { $1 }); - }; + return (Action<$0>)Delegate.CreateDelegate(typeof(Action<$0>), instance, m); } ".Replace("$0", g).Replace("$1", a); @@ -143,7 +134,7 @@ namespace UniJSON throw new ArgumentException(string.Format(""{0} is not static"", m)); } - return ($1) => (T)m.Invoke(null, new object[] { $1 }); + return (Func<$0, T>)Delegate.CreateDelegate(typeof(Func<$0, T>), null, m); } ".Replace("$0", g).Replace("$1", a); @@ -166,7 +157,7 @@ namespace UniJSON throw new ArgumentException(string.Format(""{0} is static"", m)); } - return (s, $1) => (T)m.Invoke(s, new object[] { $1 }); + return (Func)Delegate.CreateDelegate(typeof(Func), m); } ".Replace("$0", g).Replace("$1", a); @@ -189,7 +180,7 @@ namespace UniJSON throw new ArgumentException(string.Format(""{0} is static"", m)); } - return ($1) => (T)m.Invoke(instance, new object[] { $1 }); + return (Func<$0, T>)Delegate.CreateDelegate(typeof(Func<$0, T>), instance, m); } ".Replace("$0", g).Replace("$1", a); @@ -218,10 +209,7 @@ namespace UniJSON throw new ArgumentException(string.Format("{0} is not static", m)); } - return () => - { - m.Invoke(null, new object[] { }); - }; + return (Action)Delegate.CreateDelegate(typeof(Action), null, m); } public static Action OpenAction(MethodInfo m) diff --git a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs index 209b49be0..88024387c 100644 --- a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs +++ b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs @@ -17,10 +17,7 @@ namespace UniJSON throw new ArgumentException(string.Format("{0} is not static", m)); } - return (a0) => - { - m.Invoke(null, new object[] { a0 }); - }; + return (Action)Delegate.CreateDelegate(typeof(Action), null, m); } @@ -31,10 +28,7 @@ namespace UniJSON throw new ArgumentException(string.Format("{0} is not static", m)); } - return (a0, a1) => - { - m.Invoke(null, new object[] { a0, a1 }); - }; + return (Action)Delegate.CreateDelegate(typeof(Action), null, m); } @@ -45,10 +39,7 @@ namespace UniJSON throw new ArgumentException(string.Format("{0} is not static", m)); } - return (a0, a1, a2) => - { - m.Invoke(null, new object[] { a0, a1, a2 }); - }; + return (Action)Delegate.CreateDelegate(typeof(Action), null, m); } @@ -59,10 +50,7 @@ namespace UniJSON throw new ArgumentException(string.Format("{0} is not static", m)); } - return (a0, a1, a2, a3) => - { - m.Invoke(null, new object[] { a0, a1, a2, a3 }); - }; + return (Action)Delegate.CreateDelegate(typeof(Action), null, m); } //////////// OpenAction @@ -74,10 +62,7 @@ namespace UniJSON throw new ArgumentException(string.Format("{0} is static", m)); } - return (s, a0) => - { - m.Invoke(s, new object[] { a0 }); - }; + return (Action)Delegate.CreateDelegate(typeof(Action), m); } @@ -88,10 +73,7 @@ namespace UniJSON throw new ArgumentException(string.Format("{0} is static", m)); } - return (s, a0, a1) => - { - m.Invoke(s, new object[] { a0, a1 }); - }; + return (Action)Delegate.CreateDelegate(typeof(Action), m); } @@ -102,10 +84,7 @@ namespace UniJSON throw new ArgumentException(string.Format("{0} is static", m)); } - return (s, a0, a1, a2) => - { - m.Invoke(s, new object[] { a0, a1, a2 }); - }; + return (Action)Delegate.CreateDelegate(typeof(Action), m); } //////////// BindAction @@ -117,10 +96,7 @@ namespace UniJSON throw new ArgumentException(string.Format("{0} is static", m)); } - return (a0) => - { - m.Invoke(instance, new object[] { a0 }); - }; + return (Action)Delegate.CreateDelegate(typeof(Action), instance, m); } @@ -131,10 +107,7 @@ namespace UniJSON throw new ArgumentException(string.Format("{0} is static", m)); } - return (a0, a1) => - { - m.Invoke(instance, new object[] { a0, a1 }); - }; + return (Action)Delegate.CreateDelegate(typeof(Action), instance, m); } @@ -145,10 +118,7 @@ namespace UniJSON throw new ArgumentException(string.Format("{0} is static", m)); } - return (a0, a1, a2) => - { - m.Invoke(instance, new object[] { a0, a1, a2 }); - }; + return (Action)Delegate.CreateDelegate(typeof(Action), instance, m); } @@ -159,10 +129,7 @@ namespace UniJSON throw new ArgumentException(string.Format("{0} is static", m)); } - return (a0, a1, a2, a3) => - { - m.Invoke(instance, new object[] { a0, a1, a2, a3 }); - }; + return (Action)Delegate.CreateDelegate(typeof(Action), instance, m); } //////////// StaticFunc From 38a58500c9fb601f4b3035ae3ee6fa1dfe081529 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Sat, 2 Feb 2019 17:46:25 +0900 Subject: [PATCH 4/7] Remove Expression from ListTreeNodeDeserializerExtensions --- .../ListTreeNodeDeserializerExtensions.cs | 31 +++---------------- 1 file changed, 5 insertions(+), 26 deletions(-) diff --git a/Assets/VRM/UniJSON/Scripts/ListTreeNode/ListTreeNodeDeserializerExtensions.cs b/Assets/VRM/UniJSON/Scripts/ListTreeNode/ListTreeNodeDeserializerExtensions.cs index 3c6b88c86..b41eab5a0 100644 --- a/Assets/VRM/UniJSON/Scripts/ListTreeNode/ListTreeNodeDeserializerExtensions.cs +++ b/Assets/VRM/UniJSON/Scripts/ListTreeNode/ListTreeNodeDeserializerExtensions.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Linq.Expressions; using System.Reflection; @@ -30,10 +29,7 @@ namespace UniJSON var mi = typeof(GenericCreator).GetMethod("ArrayCreator", BindingFlags.NonPublic | BindingFlags.Static); var g = mi.MakeGenericMethod(t.GetElementType()); - var src = Expression.Parameter(typeof(T), "src"); - var call = Expression.Call(g, src); - var func = Expression.Lambda(call, src); - return (Func, U>)func.Compile(); + return GenericInvokeCallFactory.StaticFunc, U>(g); } { @@ -180,10 +176,7 @@ namespace UniJSON if (mi != null) { - var self = Expression.Parameter(typeof(ListTreeNode), "self"); - var call = Expression.Call(self, mi); - var func = Expression.Lambda(call, self); - return (Func, U>)func.Compile(); + return GenericInvokeCallFactory.StaticFunc, U>(mi); } } @@ -194,10 +187,7 @@ namespace UniJSON var mi = typeof(GenericDeserializer).GetMethod("GenericArrayDeserializer", BindingFlags.Static | BindingFlags.NonPublic); var g = mi.MakeGenericMethod(target.GetElementType()); - var self = Expression.Parameter(typeof(ListTreeNode), "self"); - var call = Expression.Call(g, self); - var func = Expression.Lambda(call, self); - return (Func, U>)func.Compile(); + return GenericInvokeCallFactory.StaticFunc, U>(g); } if (target.IsGenericType) @@ -207,10 +197,7 @@ namespace UniJSON var mi = typeof(GenericDeserializer).GetMethod("GenericListDeserializer", BindingFlags.Static | BindingFlags.NonPublic); var g = mi.MakeGenericMethod(target.GetGenericArguments()); - var self = Expression.Parameter(typeof(ListTreeNode), "self"); - var call = Expression.Call(g, self); - var func = Expression.Lambda(call, self); - return (Func, U>)func.Compile(); + return GenericInvokeCallFactory.StaticFunc, U>(g); } if (target.GetGenericTypeDefinition() == typeof(Dictionary<,>) && @@ -219,15 +206,7 @@ namespace UniJSON var mi = typeof(ListTreeNodeDeserializerExtensions).GetMethod("DictionaryDeserializer", BindingFlags.Static | BindingFlags.NonPublic); var g = mi.MakeGenericMethod(typeof(T)); - var self = Expression.Parameter(typeof(ListTreeNode), "self"); - var call = Expression.Call(g, self); - var func = Expression.Lambda(call, self); - var d = (Func, object>)func.Compile(); - return (ListTreeNode s) => - { - var x = d(s); - return (U)x; - }; + return GenericInvokeCallFactory.StaticFunc, U>(g); } } From 431a53230fc8fa4ee4d5efb0125d5f00253397da Mon Sep 17 00:00:00 2001 From: ousttrue Date: Sat, 2 Feb 2019 18:22:03 +0900 Subject: [PATCH 5/7] Remove Expression from JsonObjectValidator --- .../JsonObjectValidator.cs | 58 +++++++++++-------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonObjectValidator.cs b/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonObjectValidator.cs index 2c6de6c23..e0a026014 100644 --- a/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonObjectValidator.cs +++ b/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonObjectValidator.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Linq.Expressions; using System.Reflection; + namespace UniJSON { /// @@ -306,27 +306,13 @@ namespace UniJSON } public static void CreateFieldProcessors( - string methodName, + Func creator, Dictionary processors ) { - var mi = typeof(G).GetMethod(methodName, BindingFlags.Static | BindingFlags.NonPublic); - - var t = typeof(T); foreach (var fi in GetFields()) { - var value = Expression.Parameter(t, "value"); - var fieldValue = Expression.Field(value, fi); - var compileld = Expression.Lambda(fieldValue, value).Compile(); - - var getter = Expression.Constant(compileld); - var name = Expression.Constant(fi.Name); - - var g = mi.MakeGenericMethod(fi.FieldType); - var call = Expression.Call(g, getter, name); - var lambda = (Func)Expression.Lambda(call).Compile(); - - processors.Add(fi.Name, lambda()); + processors.Add(fi.Name, creator(fi)); } } } @@ -346,12 +332,23 @@ namespace UniJSON Dictionary m_validators; - static FieldValidator CreateFieldValidator(Func getter, string name) + static FieldValidator CreateFieldValidator(FieldInfo fi) { + var mi = typeof(ObjectValidator).GetMethod("_CreateFieldValidator", + BindingFlags.Static | BindingFlags.NonPublic) + ; + var g = mi.MakeGenericMethod(fi.FieldType); + return (FieldValidator)g.Invoke(null, new object[] { fi }); + } + + static FieldValidator _CreateFieldValidator(FieldInfo fi) + { + var getter = (Func)((t) => (U)fi.GetValue(t)); + return (JsonSchema s, JsonSchemaValidationContext c, T o, out bool isIgnorable) => { var v = s.Validator; - using (c.Push(name)) + using (c.Push(fi.Name)) { var field = getter(o); var ex = v.Validate(c, field); @@ -367,7 +364,7 @@ namespace UniJSON { var validators = new Dictionary(); GenericFieldView.CreateFieldProcessors( - "CreateFieldValidator", validators); + CreateFieldValidator, validators); m_validators = validators; } @@ -496,14 +493,27 @@ namespace UniJSON Dictionary m_serializers; - static FieldSerializer CreateFieldSerializer(Func getter, string name) + static FieldSerializer CreateFieldSerializer(FieldInfo fi) { + var mi = typeof(Serializer).GetMethod("_CreateFieldSerializer", + BindingFlags.Static | BindingFlags.NonPublic); + var g = mi.MakeGenericMethod(fi.FieldType); + return (FieldSerializer)g.Invoke(null, new object[] { fi }); + } + + static FieldSerializer _CreateFieldSerializer(FieldInfo fi) + { + Func getter = t => + { + return (U)fi.GetValue(t); + }; + return (s, c, f, o, vRes, deps) => { var v = s.Validator; var field = getter(o); - if (vRes[name].Ex != null) + if (vRes[fi.Name].Ex != null) { return; } @@ -519,7 +529,7 @@ namespace UniJSON } } - f.Key(name); + f.Key(fi.Name); v.Serialize(f, c, field); }; } @@ -528,7 +538,7 @@ namespace UniJSON { var serializers = new Dictionary(); GenericFieldView.CreateFieldProcessors( - "CreateFieldSerializer", serializers); + CreateFieldSerializer, serializers); m_serializers = serializers; } From a5800d593793590df13195d6f2b77ebfcaa9f893 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Sat, 2 Feb 2019 18:36:37 +0900 Subject: [PATCH 6/7] Replace Invoke with GenericInvokeCallFactory --- Assets/VRM/UniJSON/Scripts/GenericCast.cs | 5 +---- .../UniJSON/Scripts/JsonSchemaValidator/JsonEnumValidator.cs | 4 +++- .../Scripts/JsonSchemaValidator/JsonObjectValidator.cs | 4 ++-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Assets/VRM/UniJSON/Scripts/GenericCast.cs b/Assets/VRM/UniJSON/Scripts/GenericCast.cs index 890172611..fafc29bd2 100644 --- a/Assets/VRM/UniJSON/Scripts/GenericCast.cs +++ b/Assets/VRM/UniJSON/Scripts/GenericCast.cs @@ -56,10 +56,7 @@ namespace UniJSON } else { - return (Func)((S s) => - { - return (T)mi.Invoke(null, new object[] { s }); - }); + return GenericInvokeCallFactory.StaticFunc(mi); } } diff --git a/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonEnumValidator.cs b/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonEnumValidator.cs index 4cd4ec7d8..2559e3ee6 100644 --- a/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonEnumValidator.cs +++ b/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonEnumValidator.cs @@ -319,9 +319,11 @@ namespace UniJSON var mi = typeof(Enum).GetMethods(BindingFlags.Static | BindingFlags.Public).First( x => x.Name == "Parse" && x.GetParameters().Length == 3 ); + + var enumParse = GenericInvokeCallFactory.StaticFunc(mi); s_d = x => { - var enumValue = mi.Invoke(null, new object[] { typeof(U), x.GetString(), true }); + var enumValue = enumParse(typeof(U), x.GetString(), true); return GenericCast.Cast(enumValue); }; } diff --git a/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonObjectValidator.cs b/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonObjectValidator.cs index e0a026014..2c584e181 100644 --- a/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonObjectValidator.cs +++ b/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonObjectValidator.cs @@ -338,7 +338,7 @@ namespace UniJSON BindingFlags.Static | BindingFlags.NonPublic) ; var g = mi.MakeGenericMethod(fi.FieldType); - return (FieldValidator)g.Invoke(null, new object[] { fi }); + return GenericInvokeCallFactory.StaticFunc(g)(fi); } static FieldValidator _CreateFieldValidator(FieldInfo fi) @@ -498,7 +498,7 @@ namespace UniJSON var mi = typeof(Serializer).GetMethod("_CreateFieldSerializer", BindingFlags.Static | BindingFlags.NonPublic); var g = mi.MakeGenericMethod(fi.FieldType); - return (FieldSerializer)g.Invoke(null, new object[] { fi }); + return GenericInvokeCallFactory.StaticFunc(g)(fi); } static FieldSerializer _CreateFieldSerializer(FieldInfo fi) From cc9de9111a9a23dc0e7c42f9125dcb4dc148b67d Mon Sep 17 00:00:00 2001 From: ousttrue Date: Sun, 3 Feb 2019 16:44:47 +0900 Subject: [PATCH 7/7] Fixed IFormatter.KeyValue. All Expression.Compile Removed --- .../UniJSON/Scripts/FormatterExtensions.cs | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/Assets/VRM/UniJSON/Scripts/FormatterExtensions.cs b/Assets/VRM/UniJSON/Scripts/FormatterExtensions.cs index 467a588f4..dc76110ca 100644 --- a/Assets/VRM/UniJSON/Scripts/FormatterExtensions.cs +++ b/Assets/VRM/UniJSON/Scripts/FormatterExtensions.cs @@ -45,23 +45,20 @@ namespace UniJSON return method.MakeGenericMethod(typeof(T)); } - [Obsolete("error when AOT. use Key, Value")] + // + // https://stackoverflow.com/questions/238765/given-a-type-expressiontype-memberaccess-how-do-i-get-the-field-value + // public static void KeyValue(this IFormatter f, Expression> expression) { - var func = expression.Compile(); - var value = func(); - if (value != null) - { - var body = expression.Body as MemberExpression; - if (body == null) - { - body = ((UnaryExpression)expression.Body).Operand as MemberExpression; - } - f.Key(body.Member.Name); - f.Serialize(expression.Compile()()); - //var method = GetMethod(expression); - //method.Invoke(this, new object[] { value }); - } + MemberExpression outerMember = (MemberExpression)expression.Body; + var outerProp = (FieldInfo)outerMember.Member; + MemberExpression innerMember = (MemberExpression)outerMember.Expression; + var innerField = (FieldInfo)innerMember.Member; + ConstantExpression ce = (ConstantExpression)innerMember.Expression; + object innerObj = ce.Value; + object outerObj = innerField.GetValue(innerObj); + f.Key(outerProp.Name); + f.Serialize(outerProp.GetValue(outerObj)); } } }