Пример #1
0
        protected object DeserializeInternal(teStructuredDataMgr manager, teStructuredData data, STUField_Info field, FieldInfo target)
        {
            if (manager.Factories.TryGetValue(target.FieldType, out var factory))
            {
                return(factory.Deserialize(data, field));
            }

            if (typeof(ISerializable_STU).IsAssignableFrom(target.FieldType))
            {
                ISerializable_STU ret = (ISerializable_STU)Activator.CreateInstance(target.FieldType);
                ret.Deserialize(data, field);

                return(ret);
            }

            if (target.FieldType.IsEnum)
            {
                IStructuredDataPrimitiveFactory enumFactory = manager.Factories[target.FieldType.GetEnumUnderlyingType()];
                return(enumFactory.Deserialize(data, field));
            }

            bool isStruct = target.FieldType.IsValueType && !target.FieldType.IsPrimitive;

            if (isStruct)
            {
                MethodInfo method = typeof(Extensions).GetMethod(nameof(Extensions.Read))?.MakeGenericMethod(target.FieldType);
                return(method?.Invoke(data.Data, new object[] { data.Data }));
            }

            throw new NotImplementedException();
        }
Пример #2
0
        protected object DeserializeArrayInternal(teStructuredDataMgr manager, teStructuredData data, STUField_Info field, Array target)
        {
            Type elementType = target.GetType().GetElementType();

            if (elementType == null)
            {
                throw new InvalidDataException("elementType is null");
            }
            if (manager.Factories.ContainsKey(elementType))
            {
                IStructuredDataPrimitiveFactory factory = manager.Factories[elementType];
                return(factory.DeserializeArray(data, field));
            }

            if (typeof(ISerializable_STU).IsAssignableFrom(elementType))
            {
                ISerializable_STU ret = (ISerializable_STU)Activator.CreateInstance(elementType);
                ret.Deserialize_Array(data, field);

                return(ret);
            }

            if (elementType.IsEnum)
            {
                IStructuredDataPrimitiveFactory factory = manager.Factories[elementType.GetEnumUnderlyingType()];
                return(Enum.ToObject(elementType, factory.DeserializeArray(data, field)));
            }

            bool isStruct = elementType.IsValueType && !elementType.IsPrimitive;

            if (isStruct)
            {
                MethodInfo method = typeof(Extensions).GetMethod(nameof(Extensions.Read))?.MakeGenericMethod(elementType);
                return(method?.Invoke(data.DynData, new object[] { data.DynData }));
            }

            throw new NotImplementedException();
        }