diff --git a/Assets/VRM/UniJSON/Editor/Tests/GenericCallUtilityTests.cs b/Assets/VRM/UniJSON/Editor/Tests/GenericCallUtilityTests.cs index 9a6f73b22..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 = (Action)GenericInvokeCallFactory.Create(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 = (Action)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/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)); } } } diff --git a/Assets/VRM/UniJSON/Scripts/FormatterExtensionsSerializer.cs b/Assets/VRM/UniJSON/Scripts/FormatterExtensionsSerializer.cs index ca5b873c8..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.Create(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.Create(mi); + return GenericInvokeCallFactory.OpenAction(mi); } } catch (AmbiguousMatchException) @@ -111,7 +111,7 @@ namespace UniJSON if (idictionary != null) { var mi = FormatterExtensionsSerializer.GetMethod("SerializeDictionary"); - return GenericInvokeCallFactory.Create(mi); + return GenericInvokeCallFactory.StaticAction(mi); } } @@ -120,7 +120,7 @@ namespace UniJSON if (t == typeof(object[])) { var mi = FormatterExtensionsSerializer.GetMethod("SerializeObjectArray"); - return GenericInvokeCallFactory.Create(mi); + return GenericInvokeCallFactory.StaticAction(mi); } } @@ -134,7 +134,7 @@ namespace UniJSON { var g = FormatterExtensionsSerializer.GetMethod("SerializeArray"); var mi = g.MakeGenericMethod(ienumerable.GetGenericArguments()); - return GenericInvokeCallFactory.Create(mi); + return GenericInvokeCallFactory.StaticAction(mi); } } diff --git a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.cs index 76cd2528c..4a543029f 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,36 +50,22 @@ namespace UniJSON { "); - // CreateWithThis - w.WriteLine("//////////// Create"); - - // Create - 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 Create(MethodInfo m) + public static Action<$0> StaticAction<$0>(MethodInfo m) { - Action callback = null; - if (m.IsStatic) + if (!m.IsStatic) { - callback = (s, $1) => - { - m.Invoke(null, new object[] { s, $1 }); - }; + throw new ArgumentException(string.Format(""{0} is not static"", m)); } - else - { - callback = (s, $1) => - { - m.Invoke(s, new object[] { $1 }); - }; - } - return callback; + + return (Action<$0>)Delegate.CreateDelegate(typeof(Action<$0>), null, m); } ".Replace("$0", g).Replace("$1", a); @@ -79,42 +73,119 @@ namespace UniJSON } - - // CreateWithThis - w.WriteLine("//////////// CreateWithThis"); - for (int i = 1; i <= ARGS && i<= NET35MAX; ++i) + // 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<$0> CreateWithThis(MethodInfo m, S instance) + 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<$0> callback= - ($1) => { - m.Invoke(instance, new object[]{ $1 }); - }; - return callback; + return (Action)Delegate.CreateDelegate(typeof(Action), m); } ".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 (Action<$0>)Delegate.CreateDelegate(typeof(Action<$0>), instance, m); + } +".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 (Func<$0, T>)Delegate.CreateDelegate(typeof(Func<$0, T>), null, m); + } +".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 (Func)Delegate.CreateDelegate(typeof(Func), m); + } +".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 (Func<$0, T>)Delegate.CreateDelegate(typeof(Func<$0, T>), instance, m); + } +".Replace("$0", g).Replace("$1", a); + + w.WriteLine(source); + } @@ -130,22 +201,74 @@ namespace UniJSON } #endif - #region no arguments - public static Action Create(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 (Action)Delegate.CreateDelegate(typeof(Action), null, m); + } + + 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 Action CreateWithThis(MethodInfo m, S instance) + public static Action BindAction(MethodInfo m, S instance) { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + 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 ce26ca6d5..88024387c 100644 --- a/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs +++ b/Assets/VRM/UniJSON/Scripts/GenericCallUtility/GenericInvokeCallFactory.g.cs @@ -8,173 +8,252 @@ namespace UniJSON public static partial class GenericInvokeCallFactory { -//////////// Create +//////////// StaticAction - public static Action Create(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 }); - }; + throw new ArgumentException(string.Format("{0} is not static", m)); } - else - { - callback = (s, a0) => - { - m.Invoke(s, new object[] { a0 }); - }; - } - return callback; + + return (Action)Delegate.CreateDelegate(typeof(Action), null, m); } - public static Action Create(MethodInfo m) + public static Action StaticAction(MethodInfo m) { - Action callback = null; - if (m.IsStatic) + if (!m.IsStatic) { - callback = (s, a0, a1) => - { - m.Invoke(null, new object[] { s, a0, a1 }); - }; + throw new ArgumentException(string.Format("{0} is not static", m)); } - else - { - callback = (s, a0, a1) => - { - m.Invoke(s, new object[] { a0, a1 }); - }; - } - return callback; + + return (Action)Delegate.CreateDelegate(typeof(Action), null, m); } - public static Action Create(MethodInfo m) + public static Action StaticAction(MethodInfo m) { - Action callback = null; - if (m.IsStatic) + 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 CreateWithThis(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 callback; + return (Action)Delegate.CreateDelegate(typeof(Action), null, m); } - public static Action CreateWithThis(MethodInfo m, S instance) + public static Action StaticAction(MethodInfo m) + { + if (!m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is not static", m)); + } + + return (Action)Delegate.CreateDelegate(typeof(Action), null, m); + } + +//////////// 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) => { - m.Invoke(instance, new object[]{ a0, a1 }); - }; - return callback; + return (Action)Delegate.CreateDelegate(typeof(Action), m); } - public static Action CreateWithThis(MethodInfo m, S instance) + 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) => { - m.Invoke(instance, new object[]{ a0, a1, a2 }); - }; - return callback; + return (Action)Delegate.CreateDelegate(typeof(Action), m); } - public static Action CreateWithThis(MethodInfo m, S instance) + 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 callback; + return (Action)Delegate.CreateDelegate(typeof(Action), m); + } + +//////////// BindAction + + public static Action BindAction(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (Action)Delegate.CreateDelegate(typeof(Action), instance, m); + } + + + public static Action BindAction(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (Action)Delegate.CreateDelegate(typeof(Action), instance, m); + } + + + public static Action BindAction(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (Action)Delegate.CreateDelegate(typeof(Action), instance, m); + } + + + public static Action BindAction(MethodInfo m, S instance) + { + if (m.IsStatic) + { + throw new ArgumentException(string.Format("{0} is static", m)); + } + + return (Action)Delegate.CreateDelegate(typeof(Action), instance, m); + } + +//////////// 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/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/JsonArrayValidator.cs b/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonArrayValidator.cs index 2859ebd2b..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.Create< + GenericInvokeCallFactory.StaticAction< IJsonSchemaValidator, IFormatter, JsonSchemaValidationContext, 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 2c6de6c23..2c584e181 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 GenericInvokeCallFactory.StaticFunc(g)(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 GenericInvokeCallFactory.StaticFunc(g)(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; } 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); } }