Beispiel #1
0
        public object Deserialize(TextReader stream, System.Type type)
        {
            var keyTypeGetter = GetKeyTypeGetter(type);

            var(info, context) = CreateInfoAndContext(type);
            ReadTo(stream, info, keyTypeGetter);

            object inst = _instanceCreator?.Desirialize(type, info, context);

            if (inst != null)
            {
                return(inst);
            }
            else if (type.Equals(DEFAULT_TYPE))
            {
                var dict = new Dictionary <string, object>();
                foreach (var v in info.GetEnumerable())
                {
                    dict.Add(v.Name, v.Value);
                }
            }
            else if (type.ContainsInterface <ISerializable>())
            {
                var ctor = type.GetConstructor(new[] { typeof(SerializationInfo), typeof(StreamingContext) });
                Assert.IsNotNull(ctor, $"Not Define Constractor(SerializationInfo, StreamingContext)... type={type}");
                inst = ctor.Invoke(new object[] { info, context });
            }
            else if (type.GetCustomAttribute <System.SerializableAttribute>() != null ||
                     type.IsStruct())
            {
                //空引数のコンストラクを持つものだけ対応している
                var ctor = type.GetConstructor(new System.Type[] { });
                //var ctor = type.GetConstructor(null);
                Assert.IsNotNull(ctor, $"Not Define Default Constractor()... type={type}");
                Assert.AreEqual(0, ctor.GetParameters().Length, $"Not Define Default Constractor()... type={type} argsCount={ctor.GetParameters().Length}");

                inst = ctor.Invoke(new object[] { });
                foreach (var fieldInfo in inst.GetSerializedFieldEnumerable()
                         .Where(_i => info.GetEnumerable().Any(_e => _e.Name == _i.FieldInfo.Name)))
                {
                    //Debug.Log($"debug -- Desirialize -- {fieldInfo.FieldInfo.Name} type={fieldInfo.FieldInfo.FieldType}");
                    var value = info.GetValue(fieldInfo.FieldInfo.Name, fieldInfo.FieldInfo.FieldType);
                    fieldInfo.FieldInfo.SetValue(inst, value);
                }
            }
            else
            {
                Debug.LogWarning($"This type({type.FullName}) don't deserialize...");
            }
            return(inst);
        }