Example #1
0
        public override object Deserialize(DmSerializer dmSerializer, Type objType, bool isDebugMode = false)
        {
            var writer = dmSerializer.DebugWriter;
            var br     = dmSerializer.Reader;

            // Get key / value base type
            var    args            = objType.GetGenericArguments();
            Type   keyType         = args[0].GetBaseType();
            Type   valueType       = args[1].GetBaseType();
            String keyName         = keyType.Name;
            String valueName       = valueType.Name;
            var    keySerializer   = TypeSerializer.GetSerializer(keyType);
            var    valueSerializer = TypeSerializer.GetSerializer(valueType);

            // Create instance
            var arrayInstance = objType.CreateInstance() as IDictionary;

            var count = br.ReadInt32();

            for (int i = 0; i < count; i++)
            {
                var keyValue  = dmSerializer.GetObject(isDebugMode);
                var itemValue = dmSerializer.GetObject(isDebugMode);

                if (keyValue != null)
                {
                    arrayInstance.Add(keyValue, itemValue);
                }
            }

            return(arrayInstance);
        }
Example #2
0
        public override object Deserialize(DmSerializer dmSerializer, Type objType, bool isDebugMode = false)
        {
            Object instance = objType.CreateInstance();

            var currentType = objType;

            while (currentType != null && currentType != typeof(Object))

            {
                List <MemberInfo> members = DmUtils.GetMembers(currentType);

                var membersValue = new object[members.Count];

                for (int i = 0; i < members.Count; i++)
                {
                    var member      = members[i];
                    var memberValue = dmSerializer.GetObject(isDebugMode);

                    if (memberValue != null)
                    {
                        member.SetValue(instance, memberValue);
                    }
                }
                currentType = currentType.GetTypeInfo().BaseType;
            }

            return(instance);
        }
        public override object Deserialize(DmSerializer dmSerializer, Type objType, bool isDebugMode = false)
        {
            var             br = dmSerializer.Reader;
            ConstructorInfo constructorInfo = objType.GetISerializableConstructor();

            var context = new StreamingContext();
            SerializationInfo serializationInfo = new SerializationInfo(objType, new FormatterConverter());

            var count = br.ReadInt32();


            for (int i = 0; i < count; i++)
            {
                var entryName  = (string)dmSerializer.GetObject(isDebugMode);
                var entryValue = dmSerializer.GetObject(isDebugMode);

                serializationInfo.AddValue(entryName, entryValue, entryValue.GetType());
            }

            var obj = constructorInfo.Invoke(new object[] { serializationInfo, context });

            return(obj);
        }
Example #4
0
        public override object Deserialize(DmSerializer dmSerializer, Type objType, bool isDebugMode = false)
        {
            Object            instance = objType.CreateInstance();
            List <MemberInfo> members  = DmUtils.GetMembers(objType);

            var membersValue = new object[members.Count];

            for (int i = 0; i < members.Count; i++)
            {
                var member      = members[i];
                var memberValue = dmSerializer.GetObject(isDebugMode);

                if (memberValue != null)
                {
                    member.SetValue(instance, memberValue);
                }
            }


            return(instance);
        }
        public override object Deserialize(DmSerializer dmSerializer, Type objType, bool isDebugMode = false)
        {
            var br = dmSerializer.Reader;

            // DOnt try to get an ICollection (coz ICollection<T> doesn't inherit from it)
            object arrayInstance;

            // Create instance
            if (objType.IsArray)
            {
                arrayInstance = new ArrayList();
            }
            else
            {
                arrayInstance = objType.CreateInstance();
            }

            var count = br.ReadInt32();

            // Get the Add method if exist
            // Since ICollection<T> is not accessible
            var valueType = GetArrayItemValueType(objType);

            Func <object, int> addFunction = null;

            if (objType.IsArray)
            {
                addFunction = ((ArrayList)arrayInstance).Add;
            }
            else
            {
                MethodInfo mi = objType.GetMethod("Add", new Type[] { valueType });

                if (mi == null)
                {
                    throw new ArgumentException("Deserializing an array must have a Add<T> method");
                }

                addFunction = new Func <object, int>((value) =>
                {
                    mi.Invoke(arrayInstance, new object[] { value });
                    return(0);
                });
            }


            for (int i = 0; i < count; i++)
            {
                var item = dmSerializer.GetObject(isDebugMode);

                addFunction(item);
            }

            if (objType.IsArray)
            {
                return(((ArrayList)arrayInstance).ToArray(objType.GetTypeInfo().GetElementType()));
            }
            else
            {
                return(arrayInstance);
            }
        }