diff --git a/Assets/VRM/UniJSON/Scripts/ConcreteCast.cs b/Assets/VRM/UniJSON/Scripts/ConcreteCast.cs index 912cc2c37..1bbdd996d 100644 --- a/Assets/VRM/UniJSON/Scripts/ConcreteCast.cs +++ b/Assets/VRM/UniJSON/Scripts/ConcreteCast.cs @@ -2,6 +2,7 @@ using System.IO; using System.Text; using UnityEngine; +using System.Reflection; #if UNITY_EDITOR using UnityEditor; #endif @@ -11,6 +12,19 @@ namespace UniJSON { public static partial class ConcreteCast { + public static string GetMethodName(Type src, Type dst) + { + return string.Format("Cast{0}To{1}", src.Name, dst.Name); + } + + public static MethodInfo GetMethod(Type src, Type dst) + { + var name = GetMethodName(src, dst); + var mi = typeof(ConcreteCast).GetMethod(name, + BindingFlags.Static | BindingFlags.Public); + return mi; + } + #if UNITY_EDITOR static Type[] s_castTypes = new Type[] @@ -47,11 +61,11 @@ namespace UniJSON { foreach (var y in s_castTypes) { w.WriteLine(@" - public static $1 Cast$0To$1($0 src) + public static $1 $2($0 src) { return ($1)src; } -".Replace("$0", x.Name).Replace("$1", y.Name)); +".Replace("$0", x.Name).Replace("$1", y.Name).Replace("$2", GetMethodName(x, y))); } } w.WriteLine(@" diff --git a/Assets/VRM/UniJSON/Scripts/GenericCast.cs b/Assets/VRM/UniJSON/Scripts/GenericCast.cs index 69cb0219f..890172611 100644 --- a/Assets/VRM/UniJSON/Scripts/GenericCast.cs +++ b/Assets/VRM/UniJSON/Scripts/GenericCast.cs @@ -1,11 +1,4 @@ using System; -using System.IO; -using System.Linq.Expressions; -using System.Text; -#if UNITY_EDITOR -using UnityEditor; -using UnityEngine; -#endif namespace UniJSON @@ -53,33 +46,30 @@ namespace UniJSON { public static Func CreateCast() { - if (typeof(S) == typeof(T)) + var mi = ConcreteCast.GetMethod(typeof(S), typeof(T)); + if (mi == null) { - // through - var src = Expression.Parameter(typeof(S), "src"); - var lambda = Expression.Lambda(src, src); - return (Func)lambda.Compile(); + return (Func)((S s) => + { + return (T)(object)s; + }); } else { - // cast - var src = Expression.Parameter(typeof(S), "src"); - var cast = Expression.Convert(src, typeof(T)); - var lambda = Expression.Lambda(cast, src); - return (Func)lambda.Compile(); + return (Func)((S s) => + { + return (T)mi.Invoke(null, new object[] { s }); + }); } } public static Func> CreateConst() { - var src = Expression.Parameter(typeof(S), "src"); - var convert = Expression.Convert(src, typeof(T)); - var lambda = (Func)Expression.Lambda(convert, src).Compile(); - return s => + var cast = CreateCast(); + return (Func>)((S s) => { - var t = lambda(s); - return () => t; - }; + return (Func)(() => cast(s)); + }); } } }