Remove Expression from GenericCast

This commit is contained in:
ousttrue 2019-02-01 22:19:56 +09:00
parent 0a55f6fb29
commit 6268662829
2 changed files with 30 additions and 26 deletions

View File

@ -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(@"

View File

@ -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<S, T> CreateCast<S, T>()
{
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<S, T>)lambda.Compile();
return (Func<S, T>)((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<S, T>)lambda.Compile();
return (Func<S, T>)((S s) =>
{
return (T)mi.Invoke(null, new object[] { s });
});
}
}
public static Func<S, Func<T>> CreateConst<S, T>()
{
var src = Expression.Parameter(typeof(S), "src");
var convert = Expression.Convert(src, typeof(T));
var lambda = (Func<S, T>)Expression.Lambda(convert, src).Compile();
return s =>
var cast = CreateCast<S, T>();
return (Func<S, Func<T>>)((S s) =>
{
var t = lambda(s);
return () => t;
};
return (Func<T>)(() => cast(s));
});
}
}
}