Refactor GenericInvokeCallFactory. StaticAction/Func, OpenAction/Func, BindAction/Func

This commit is contained in:
ousttrue
2019-02-02 17:27:28 +09:00
parent 49b57e7ef3
commit da562a18c0
5 changed files with 475 additions and 219 deletions

View File

@@ -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<Sample, int>(mi);
invoke(s, 1);
Assert.AreEqual(1, s.Value);
{
var mi = s.GetType().GetMethod("Set");
var action = GenericInvokeCallFactory.OpenAction<Sample, int>(mi);
action(s, 1);
Assert.AreEqual(1, s.Value);
}
var exp = GenericExpressionCallFactory.Create<Sample, int>(mi);
exp(s, 2);
Assert.AreEqual(2, s.Value);
{
var mi = s.GetType().GetMethod("Get");
var func = GenericInvokeCallFactory.OpenFunc<Sample, int, int>(mi);
var value = func(s, 1);
Assert.AreEqual(1, s.Value);
}
{
var mi = s.GetType().GetMethod("Set");
var action = GenericExpressionCallFactory.Create<Sample, int>(mi);
action(s, 2);
Assert.AreEqual(2, s.Value);
}
}
}
}

View File

@@ -84,7 +84,7 @@ namespace UniJSON
if (typeof(T) == typeof(object) && t.GetType() != typeof(object))
{
var mi = FormatterExtensionsSerializer.GetMethod("SerializeObject");
return GenericInvokeCallFactory.CreateAction<IFormatter, T>(mi);
return GenericInvokeCallFactory.StaticAction<IFormatter, T>(mi);
}
try
@@ -93,7 +93,7 @@ namespace UniJSON
var mi = typeof(IFormatter).GetMethod("Value", new Type[] { t });
if (mi != null)
{
return GenericInvokeCallFactory.CreateAction<IFormatter, T>(mi);
return GenericInvokeCallFactory.OpenAction<IFormatter, T>(mi);
}
}
catch (AmbiguousMatchException)
@@ -111,7 +111,7 @@ namespace UniJSON
if (idictionary != null)
{
var mi = FormatterExtensionsSerializer.GetMethod("SerializeDictionary");
return GenericInvokeCallFactory.CreateAction<IFormatter, T>(mi);
return GenericInvokeCallFactory.StaticAction<IFormatter, T>(mi);
}
}
@@ -120,7 +120,7 @@ namespace UniJSON
if (t == typeof(object[]))
{
var mi = FormatterExtensionsSerializer.GetMethod("SerializeObjectArray");
return GenericInvokeCallFactory.CreateAction<IFormatter, T>(mi);
return GenericInvokeCallFactory.StaticAction<IFormatter, T>(mi);
}
}
@@ -134,7 +134,7 @@ namespace UniJSON
{
var g = FormatterExtensionsSerializer.GetMethod("SerializeArray");
var mi = g.MakeGenericMethod(ienumerable.GetGenericArguments());
return GenericInvokeCallFactory.CreateAction<IFormatter, T>(mi);
return GenericInvokeCallFactory.StaticAction<IFormatter, T>(mi);
}
}

View File

@@ -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
{
/// <summary>
/// MethodInfoからDelegateを作成する
///
/// * StaticAction/Func StaticMethod呼び出し
/// * OpenAction/Func 第1引数にthisを受けるメソッド呼び出し
/// * BindAction/Func thisを内部に保持したメソッド呼び出し
///
/// </summary>
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<S, $0> CreateAction<S, $0>(MethodInfo m)
{
Action<S, $0> 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<S, $0>(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<S, $0> OpenAction<S, $0>(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<S, $0>(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<S, $0, T> OpenFunc<S, $0, T>(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<S, $0, T>(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<S> CreateAction<S>(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<S> OpenAction<S>(MethodInfo m)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return (s) =>
{
m.Invoke(s, new object[] { });
};
}
public static Func<S, T> CreateFunc<S, T>(MethodInfo m)
public static Action BindAction<S>(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<S>(MethodInfo m, S instance)
{
return () =>
{
m.Invoke(instance, new object[] { });
};
}
#endregion
#region Func without arguments
public static Func<T> StaticFunc<T>(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<S, T> OpenFunc<S, T>(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<T> BindFunc<S, T>(MethodInfo m, S instance)
{
if (m.IsStatic)
{
throw new ArgumentException(string.Format("{0} is static", m));
}
return () => (T)m.Invoke(instance, new object[] { });
}
#endregion
}
}

View File

@@ -8,173 +8,285 @@ namespace UniJSON
public static partial class GenericInvokeCallFactory
{
//////////// CreateAction
//////////// StaticAction
public static Action<S, A0> CreateAction<S, A0>(MethodInfo m)
public static Action<A0> StaticAction<A0>(MethodInfo m)
{
Action<S, A0> 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<S, A0, A1> CreateAction<S, A0, A1>(MethodInfo m)
{
Action<S, A0, A1> 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<S, A0, A1, A2> CreateAction<S, A0, A1, A2>(MethodInfo m)
{
Action<S, A0, A1, A2> 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<A0> CreateActionBindThis<S, A0>(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<A0> callback=
(a0) => {
m.Invoke(instance, new object[]{ a0 });
return (a0) =>
{
m.Invoke(null, new object[] { a0 });
};
return callback;
}
public static Action<A0, A1> CreateActionBindThis<S, A0, A1>(MethodInfo m, S instance)
public static Action<A0, A1> StaticAction<A0, A1>(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<A0, A1> 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<A0, A1, A2> CreateActionBindThis<S, A0, A1, A2>(MethodInfo m, S instance)
public static Action<A0, A1, A2> StaticAction<A0, A1, A2>(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<A0, A1, A2> 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<A0, A1, A2, A3> CreateActionBindThis<S, A0, A1, A2, A3>(MethodInfo m, S instance)
public static Action<A0, A1, A2, A3> StaticAction<A0, A1, A2, A3>(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<S, A0> OpenAction<S, A0>(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<A0, A1, A2, A3> 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<S, A0, A1> OpenAction<S, A0, A1>(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<S, A0, A1, A2> OpenAction<S, A0, A1, A2>(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<A0> BindAction<S, A0>(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<A0, A1> BindAction<S, A0, A1>(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<A0, A1, A2> BindAction<S, A0, A1, A2>(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<A0, A1, A2, A3> BindAction<S, A0, A1, A2, A3>(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<A0, T> StaticFunc<A0, T>(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<A0, A1, T> StaticFunc<A0, A1, T>(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<A0, A1, A2, T> StaticFunc<A0, A1, A2, T>(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<A0, A1, A2, A3, T> StaticFunc<A0, A1, A2, A3, T>(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<S, A0, T> OpenFunc<S, A0, T>(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<S, A0, A1, T> OpenFunc<S, A0, A1, T>(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<S, A0, A1, A2, T> OpenFunc<S, A0, A1, A2, T>(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<A0, T> BindFunc<S, A0, T>(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<A0, A1, T> BindFunc<S, A0, A1, T>(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<A0, A1, A2, T> BindFunc<S, A0, A1, A2, T>(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<A0, A1, A2, A3, T> BindFunc<S, A0, A1, A2, A3, T>(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 });
}

View File

@@ -264,7 +264,7 @@ namespace UniJSON
IFormatter,
JsonSchemaValidationContext,
T>)
GenericInvokeCallFactory.CreateAction<
GenericInvokeCallFactory.StaticAction<
IJsonSchemaValidator,
IFormatter,
JsonSchemaValidationContext,