mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-05-20 01:37:59 -05:00
Remove Expression from GenericCast
This commit is contained in:
parent
0a55f6fb29
commit
6268662829
|
|
@ -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(@"
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user