Files
crtr/Assets/Cryville/Common/Plist/PlistConvert.cs
2022-09-30 17:32:21 +08:00

51 lines
1.5 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
namespace Cryville.Common.Plist {
public class PlistConvert {
public static T Deserialize<T>(string file) {
return (T)Deserialize(typeof(T), PlistCS.Plist.readPlist(file));
}
public static object Deserialize(Type type, object obj, Binder binder = null) {
if (binder == null)
binder = BinderAttribute.CreateBinderOfType(type);
if (obj is IList) {
var lobj = (List<object>)obj;
foreach (var i in lobj) {
throw new NotImplementedException(); // TODO
}
}
else if (obj is IDictionary) {
var dobj = (Dictionary<string, object>)obj;
if (typeof(IDictionary).IsAssignableFrom(type)) {
var result = (IDictionary)ReflectionHelper.InvokeEmptyConstructor(type);
var it = type.GetGenericArguments()[1];
foreach (var i in dobj) {
var value = Deserialize(it, i.Value, binder);
result.Add(i.Key, value);
}
return result;
}
else {
var result = ReflectionHelper.InvokeEmptyConstructor(type);
foreach (var i in dobj) {
var imis = type.GetMember(i.Key);
if (imis.Length == 0) continue;
var imi = imis[0];
var it = ReflectionHelper.GetMemberType(imi);
var value = Deserialize(it, i.Value, binder);
ReflectionHelper.SetValue(imi, result, value, binder);
}
return result;
}
}
else return obj;
throw new Exception(); // TODO
}
}
}