GenericConstructor.cs
1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Reflection;
namespace UniJSON
{
struct GenericConstructor<T, U>
where T : IListTreeItem, IValue<T>
{
static V[] ArrayCreator<V>(ListTreeNode<T> src)
{
if (!src.IsArray())
{
throw new ArgumentException("value is not array");
}
var count = src.GetArrayCount();
return new V[count];
}
static Func<ListTreeNode<T>, U> GetCreator()
{
var t = typeof(U);
if (t.IsArray)
{
var mi = typeof(GenericConstructor<T, U>).GetMethod("ArrayCreator",
BindingFlags.NonPublic | BindingFlags.Static);
var g = mi.MakeGenericMethod(t.GetElementType());
return GenericInvokeCallFactory.StaticFunc<ListTreeNode<T>, U>(g);
}
{
return _s =>
{
return Activator.CreateInstance<U>();
};
}
}
delegate U Creator(ListTreeNode<T> src);
static Creator s_creator;
public U Create(ListTreeNode<T> src)
{
if (s_creator == null)
{
var d = GetCreator();
s_creator = new Creator(d);
}
return s_creator(src);
}
}
}