mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-05-10 20:53:31 -05:00
Separate GenericConstructor.cs
This commit is contained in:
parent
27d5a453b7
commit
ba466ff916
53
Assets/VRM/UniJSON/Scripts/GenericConstructor.cs
Normal file
53
Assets/VRM/UniJSON/Scripts/GenericConstructor.cs
Normal file
|
|
@ -0,0 +1,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Assets/VRM/UniJSON/Scripts/GenericConstructor.cs.meta
Normal file
12
Assets/VRM/UniJSON/Scripts/GenericConstructor.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6121925cab5acc54bbb0e0e213783394
|
||||
timeCreated: 1549447177
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -8,53 +8,6 @@ namespace UniJSON
|
|||
{
|
||||
public static class ListTreeNodeDeserializerExtensions
|
||||
{
|
||||
struct GenericCreator<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(GenericCreator<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);
|
||||
}
|
||||
}
|
||||
|
||||
static object DictionaryDeserializer<T>(ListTreeNode<T> s)
|
||||
where T : IListTreeItem, IValue<T>
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user