コード例 #1
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var arraySerializer = new ObjectSerializer(type);

            var elementType              = type.GetElementType();
            var elementSerializer        = serializer.GetSerializerByType(elementType);
            var preserveObjectReferences = serializer.Options.PreserveObjectReferences;

            var readGeneric  = GetType().GetTypeInfo().GetMethod(nameof(ReadValues), BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(elementType);
            var writeGeneric = GetType().GetTypeInfo().GetMethod(nameof(WriteValues), BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(elementType);

            object Reader(Stream stream, DeserializerSession session)
            {
                //Stream stream, DeserializerSession session, bool preserveObjectReferences
                var res = readGeneric.Invoke(null, new object[] { stream, session, preserveObjectReferences });

                return(res);
            }

            void Writer(Stream stream, object arr, SerializerSession session)
            {
                //T[] array, Stream stream, Type elementType, ValueSerializer elementSerializer, SerializerSession session, bool preserveObjectReferences
                writeGeneric.Invoke(null, new[] { arr, stream, elementType, elementSerializer, session, preserveObjectReferences });
            }

            arraySerializer.Initialize(Reader, Writer);
            typeMapping.TryAdd(type, arraySerializer);
            return(arraySerializer);
        }
コード例 #2
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var x = new ObjectSerializer(type);

            typeMapping.TryAdd(type, x);

            var elementType     = GetEnumerableType(type);
            var arrType         = elementType.MakeArrayType();
            var listModule      = type.Assembly.GetType("Microsoft.FSharp.Collections.ListModule");
            var ofArray         = listModule.GetMethod("OfArray");
            var ofArrayConcrete = ofArray.MakeGenericMethod(elementType);
            var ofArrayCompiled = CodeGenerator.CompileToDelegate(ofArrayConcrete, arrType);
            var toArray         = listModule.GetMethod("ToArray");
            var toArrayConcrete = toArray.MakeGenericMethod(elementType);
            var toArrayCompiled = CodeGenerator.CompileToDelegate(toArrayConcrete, type);

            ValueWriter writer = (stream, o, session) =>
            {
                var arr           = toArrayCompiled(o);
                var arrSerializer = serializer.GetSerializerByType(arrType);
                arrSerializer.WriteValue(stream, arr, session);
            };

            ValueReader reader = (stream, session) =>
            {
                var arrSerializer = serializer.GetSerializerByType(arrType);
                var items         = (Array)arrSerializer.ReadValue(stream, session);
                var res           = ofArrayCompiled(items);
                return(res);
            };

            x.Initialize(reader, writer);
            return(x);
        }
コード例 #3
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var os = new ObjectSerializer(type);

            typeMapping.TryAdd(type, os);
            ObjectReader reader = (stream, session) =>
            {
                var name  = stream.ReadString(session);
                var owner = stream.ReadObject(session) as Type;

#if NET45
                var field = owner.GetTypeInfo().GetField(name, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                return(field);
#else
                return(null);
#endif
            };
            ObjectWriter writer = (stream, obj, session) =>
            {
                var field = (FieldInfo)obj;
                var name  = field.Name;
                var owner = field.DeclaringType;
                StringSerializer.WriteValueImpl(stream, name, session);
                stream.WriteObjectWithManifest(owner, session);
            };

            os.Initialize(reader, writer);

            return(os);
        }
コード例 #4
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var os = new ObjectSerializer(type);

            typeMapping.TryAdd(type, os);
            var methodInfoSerializer     = serializer.GetSerializerByType(typeof(MethodInfo));
            var preserveObjectReferences = serializer.Options.PreserveObjectReferences;

            object Reader(Stream stream, DeserializerSession session)
            {
                var target = stream.ReadObject(session);
                var method = (MethodInfo)stream.ReadObject(session);
                var del    = method.CreateDelegate(type, target);

                return(del);
            }

            void Writer(Stream stream, object value, SerializerSession session)
            {
                var d      = (Delegate)value;
                var method = d.GetMethodInfo();

                stream.WriteObjectWithManifest(d.Target, session);
                //less lookups, slightly faster
                stream.WriteObject(method, type, methodInfoSerializer, preserveObjectReferences, session);
            }

            os.Initialize(Reader, Writer);
            return(os);
        }
コード例 #5
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var preserveObjectReferences = serializer.Options.PreserveObjectReferences;
            var ser = new ObjectSerializer(type);

            typeMapping.TryAdd(type, ser);
            var elementType       = type.GetTypeInfo().GetGenericArguments()[0];
            var elementSerializer = serializer.GetSerializerByType(elementType);
            var readGeneric       = GetType().GetTypeInfo().GetMethod(nameof(ReadHashSet), BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(elementType);
            var writeGeneric      = GetType().GetTypeInfo().GetMethod(nameof(WriteHashSet), BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(elementType);

            object Reader(Stream stream, DeserializerSession session)
            {
                var res = readGeneric.Invoke(null, new object[] { stream, session, preserveObjectReferences });

                return(res);
            }

            void Writer(Stream stream, object obj, SerializerSession session)
            {
                writeGeneric.Invoke(null, new[] { obj, stream, session, elementType, elementSerializer, preserveObjectReferences });
            }

            ser.Initialize(Reader, Writer);

            return(ser);
        }
コード例 #6
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var os = new ObjectSerializer(serializer.Options.FieldSelector, type);

            typeMapping.TryAdd(type, os);
            ObjectReader reader = (stream, session) =>
            {
                var name      = stream.ReadString(session);
                var owner     = stream.ReadObject(session) as Type;
                var arguments = stream.ReadObject(session) as Type[];

#if NET45
                var method = owner.GetMethod(name, arguments);
                return(method);
#else
                return(null);
#endif
            };
            ObjectWriter writer = (stream, obj, session) =>
            {
                var method    = (MethodInfo)obj;
                var name      = method.Name;
                var owner     = method.DeclaringType;
                var arguments = method.GetParameters().Select(p => p.ParameterType).ToArray();
                StringSerializer.WriteValueImpl(stream, name, session);
                stream.WriteObjectWithManifest(owner, session);
                stream.WriteObjectWithManifest(arguments, session);
            };

            os.Initialize(reader, writer);

            return(os);
        }
コード例 #7
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var preserveObjectReferences = serializer.Options.PreserveObjectReferences;
            var ser = new ObjectSerializer(type);

            typeMapping.TryAdd(type, ser);
            var elementSerializer = serializer.GetSerializerByType(typeof(DictionaryEntry));

            ObjectReader reader = (stream, session) =>
            {
                throw new NotSupportedException("Generic IDictionary<TKey,TValue> are not yet supported");
#pragma warning disable CS0162 // Unreachable code detected
                var instance = Activator.CreateInstance(type);
#pragma warning restore CS0162 // Unreachable code detected
                if (preserveObjectReferences)
                {
                    session.TrackDeserializedObject(instance);
                }
                var count   = stream.ReadInt32(session);
                var entries = new DictionaryEntry[count];
                for (var i = 0; i < count; i++)
                {
                    var entry = (DictionaryEntry)stream.ReadObject(session);
                    entries[i] = entry;
                }
                //TODO: populate dictionary
                return(instance);
            };

            ObjectWriter writer = (stream, obj, session) =>
            {
                if (preserveObjectReferences)
                {
                    session.TrackSerializedObject(obj);
                }
                var dict = obj as IDictionary;

                /*
                 * if (dict == null)
                 * {
                 *  Console.ForegroundColor = ConsoleColor.Yellow;
                 *  Console.WriteLine(obj.GetType().FullName);
                 *  return;
                 * }
                 * //*/
                // ReSharper disable once PossibleNullReferenceException
                Int32Serializer.WriteValueImpl(stream, dict.Count, session);
                foreach (var item in dict)
                {
                    stream.WriteObject(item, typeof(DictionaryEntry), elementSerializer,
                                       serializer.Options.PreserveObjectReferences, session);
                    // elementSerializer.WriteValue(stream,item,session);
                }
            };

            ser.Initialize(reader, writer);

            return(ser);
        }
コード例 #8
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
            ConcurrentDictionary<Type, ValueSerializer> typeMapping)
        {
            var exceptionSerializer = new ObjectSerializer(type);
            exceptionSerializer.Initialize((stream, session) =>
            {
                var exception = Activator.CreateInstance(type);
                var className = stream.ReadString(session);
                var message = stream.ReadString(session);
                var remoteStackTraceString = stream.ReadString(session);
                var stackTraceString = stream.ReadString(session);
                var innerException = stream.ReadObject(session);

                _className.SetValue(exception,className);
                _message.SetValue(exception, message);
                _remoteStackTraceString.SetValue(exception, remoteStackTraceString);
                _stackTraceString.SetValue(exception, stackTraceString);
                _innerException.SetValue(exception,innerException);
                return exception;
            }, (stream, exception, session) =>
            {
                var className = (string)_className.GetValue(exception);
                var message = (string)_message.GetValue(exception);
                var remoteStackTraceString = (string)_remoteStackTraceString.GetValue(exception);
                var stackTraceString = (string)_stackTraceString.GetValue(exception);
                var innerException = _innerException.GetValue(exception);
                StringSerializer.WriteValueImpl(stream,className,session);
                StringSerializer.WriteValueImpl(stream, message, session);
                StringSerializer.WriteValueImpl(stream, remoteStackTraceString, session);
                StringSerializer.WriteValueImpl(stream, stackTraceString, session);
                stream.WriteObjectWithManifest(innerException, session);
            });
            typeMapping.TryAdd(type, exceptionSerializer);
            return exceptionSerializer;
        }
コード例 #9
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var os = new ObjectSerializer(type);

            typeMapping.TryAdd(type, os);

            object Reader(Stream stream, DeserializerSession session)
            {
                var owner     = stream.ReadObject(session) as Type;
                var arguments = stream.ReadObject(session) as Type[];

#if NET461
                var ctor = owner.GetTypeInfo().GetConstructor(arguments);
                return(ctor);
#else
                return(null);
#endif
            }

            void Writer(Stream stream, object obj, SerializerSession session)
            {
                var ctor      = (ConstructorInfo)obj;
                var owner     = ctor.DeclaringType;
                var arguments = ctor.GetParameters().Select(p => p.ParameterType).ToArray();

                stream.WriteObjectWithManifest(owner, session);
                stream.WriteObjectWithManifest(arguments, session);
            }

            os.Initialize(Reader, Writer);

            return(os);
        }
コード例 #10
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var os = new ObjectSerializer(type);

            if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index))
            {
                var wrapper = new KnownTypeObjectSerializer(os, index);
                typeMapping.TryAdd(type, wrapper);
            }
            else
            {
                typeMapping.TryAdd(type, os);
            }
            var          methodInfoSerializer     = serializer.GetSerializerByType(typeof(MethodInfo));
            var          preserveObjectReferences = serializer.Options.PreserveObjectReferences;
            ObjectReader reader = (stream, session) =>
            {
                var target = stream.ReadObject(session);
                var method = (MethodInfo)stream.ReadObject(session);
                var del    = method.CreateDelegate(type, target);
                return(del);
            };
            ObjectWriter writer = (stream, value, session) =>
            {
                var d      = (Delegate)value;
                var method = d.GetMethodInfo();
                stream.WriteObjectWithManifest(d.Target, session);
                //less lookups, slightly faster
                stream.WriteObject(method, type, methodInfoSerializer, preserveObjectReferences, session);
            };

            os.Initialize(reader, writer);
            return(os);
        }
コード例 #11
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var os = new ObjectSerializer(type);

            typeMapping.TryAdd(type, os);

            object Reader(Stream stream, DeserializerSession session)
            {
                var name  = stream.ReadString(session);
                var owner = stream.ReadObject(session) as Type;

#if NET461
                var property = owner.GetTypeInfo()
                               .GetProperty(name, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                return(property);
#else
                return(null);
#endif
            }

            void Writer(Stream stream, object obj, SerializerSession session)
            {
                var property = (PropertyInfo)obj;
                var name     = property.Name;
                var owner    = property.DeclaringType;

                StringSerializer.WriteValueImpl(stream, name, session);
                stream.WriteObjectWithManifest(owner, session);
            }

            os.Initialize(Reader, Writer);

            return(os);
        }
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var arraySerializer = new ObjectSerializer(type);

            var elementType =
                type.GetTypeInfo()
                .GetMethods()
                .Where(methodInfo => methodInfo.Name == "Get")
                .Select(methodInfo => methodInfo.ReturnType)
                .FirstOrDefault();

            var elementSerializer        = serializer.GetSerializerByType(elementType);
            var preserveObjectReferences = serializer.Options.PreserveObjectReferences;

            var arrayRank = type.GetArrayRank();

            //TODO: code gen this part
            ObjectReader reader = CreateReader(preserveObjectReferences, arrayRank, elementType);

            ObjectWriter writer = (stream, arr, session) =>
            {
                if (preserveObjectReferences)
                {
                    session.TrackSerializedObject(arr);
                }

                WriteValues((Array)arr, stream, elementType, elementSerializer, session);
            };

            arraySerializer.Initialize(reader, writer);
            typeMapping.TryAdd(type, arraySerializer);
            return(arraySerializer);
        }
コード例 #13
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        CachedReadConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var os = new ObjectSerializer(type);

            typeMapping.TryAdd(type, os);
            ObjectReader reader = (stream, session) =>
            {
                var name  = stream.ReadString(session);
                var owner = stream.ReadObject(session) as Type;

                var property = owner.GetProperty(name, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                return(property);
            };
            ObjectWriter writer = (stream, obj, session) =>
            {
                var property = (PropertyInfo)obj;
                var name     = property.Name;
                var owner    = property.DeclaringType;
                StringSerializer.WriteValueImpl(stream, name, session);
                stream.WriteObjectWithManifest(owner, session);
            };

            os.Initialize(reader, writer);

            return(os);
        }
コード例 #14
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var os = new ObjectSerializer(type);

            typeMapping.TryAdd(type, os);
            ObjectReader reader = (stream, session) =>
            {
                var owner     = stream.ReadObject(session) as Type;
                var arguments = stream.ReadObject(session) as Type[];

                var ctor = owner.GetTypeInfo().GetConstructor(arguments);
                return(ctor);
            };
            ObjectWriter writer = (stream, obj, session) =>
            {
                var ctor      = (ConstructorInfo)obj;
                var owner     = ctor.DeclaringType;
                var arguments = ctor.GetParameters().Select(p => p.ParameterType).ToArray();
                stream.WriteObjectWithManifest(owner, session);
                stream.WriteObjectWithManifest(arguments, session);
            };

            os.Initialize(reader, writer);

            return(os);
        }
コード例 #15
0
        public void BuildSerializer(Serializer serializer, ObjectSerializer objectSerializer)
        {
            var type   = objectSerializer.Type;
            var fields = type.GetFieldInfosForType();
            var writer = GetFieldsWriter(serializer, fields, type, out var bufferSize);
            var reader = GetFieldsReader(serializer, fields, type);

            objectSerializer.Initialize(reader, writer, bufferSize);
        }
コード例 #16
0
ファイル: DefaultCodeGenerator.cs プロジェクト: tuga1975/Wire
        public void BuildSerializer([NotNull] Serializer serializer, [NotNull] ObjectSerializer objectSerializer)
        {
            var type   = objectSerializer.Type;
            var fields = type.GetFieldInfosForType();
            int preallocatedBufferSize;
            var writer = GetFieldsWriter(serializer, fields, out preallocatedBufferSize);
            var reader = GetFieldsReader(serializer, fields, type);

            objectSerializer.Initialize(reader, writer, preallocatedBufferSize);
        }
コード例 #17
0
        public void BuildSerializer([NotNull] Serializer serializer, [NotNull] ObjectSerializer objectSerializer)
        {
            var type   = objectSerializer.Type;
            var fields = serializer.Options.FieldSelector.SelectFields(type);
            int preallocatedBufferSize;
            var writer = GetFieldsWriter(serializer, fields, out preallocatedBufferSize);
            var reader = GetFieldsReader(serializer, fields, type);

            objectSerializer.Initialize(reader, writer, preallocatedBufferSize);
        }
コード例 #18
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var x = new ObjectSerializer(type);

            typeMapping.TryAdd(type, x);
            var preserveObjectReferences = serializer.Options.PreserveObjectReferences;

            var elementType       = GetEnumerableType(type) ?? typeof(object);
            var elementSerializer = serializer.GetSerializerByType(elementType);

            ValueWriter writer = (stream, o, session) =>
            {
                var enumerable = o as ICollection;
                // ReSharper disable once PossibleNullReferenceException
                stream.WriteInt32(enumerable.Count);
                foreach (var value in enumerable)
                {
                    stream.WriteObject(value, elementType, elementSerializer, preserveObjectReferences, session);
                }
            };

            ValueReader reader = (stream, session) =>
            {
                var count = stream.ReadInt32(session);
                var items = Array.CreateInstance(elementType, count);
                for (var i = 0; i < count; i++)
                {
                    var value = stream.ReadObject(session);
                    items.SetValue(value, i);
                }
                //HACK: this needs to be fixed, codegenerated or whatever
                var instance = Activator.CreateInstance(type);
                var addRange = type.GetMethod("AddRange");
                if (addRange != null)
                {
                    addRange.Invoke(instance, new object[] { items });
                    return(instance);
                }
                var add = type.GetMethod("Add");
                if (add != null)
                {
                    for (var i = 0; i < items.Length; i++)
                    {
                        add.Invoke(instance, new[] { items.GetValue(i) });
                    }
                }

                return(instance);
            };

            x.Initialize(reader, writer);
            return(x);
        }
コード例 #19
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var exceptionSerializer   = new ObjectSerializer(type);
            var hasDefaultConstructor = type.GetTypeInfo().GetConstructor(new Type[0]) != null;
            var createInstance        = hasDefaultConstructor ? Activator.CreateInstance : GetUninitializedObject;

            exceptionSerializer.Initialize((stream, session) =>
            {
                var exception = createInstance(type);
                var className = stream.ReadString(session);
                var message   = stream.ReadString(session);
                var remoteStackTraceString = stream.ReadString(session);
                var stackTraceString       = stream.ReadString(session);
                var innerException         = stream.ReadObject(session);

#if NETSTANDARD20
                _className?.SetValue(exception, className);
#else
                _className.SetValue(exception, className);
#endif
                _message.SetValue(exception, message);
                _remoteStackTraceString.SetValue(exception, remoteStackTraceString);
                _stackTraceString.SetValue(exception, stackTraceString);
                _innerException.SetValue(exception, innerException);
                return(exception);
            }, (stream, exception, session) =>
            {
#if NETSTANDARD20
                var className = (string)_className?.GetValue(exception);
#else
                var className = (string)_className.GetValue(exception);
#endif
                var message = (string)_message.GetValue(exception);
                var remoteStackTraceString = (string)_remoteStackTraceString.GetValue(exception);
                var stackTraceString       = (string)_stackTraceString.GetValue(exception);
                var innerException         = _innerException.GetValue(exception);
                StringSerializer.WriteValueImpl(stream, className, session);
                StringSerializer.WriteValueImpl(stream, message, session);
                StringSerializer.WriteValueImpl(stream, remoteStackTraceString, session);
                StringSerializer.WriteValueImpl(stream, stackTraceString, session);
                stream.WriteObjectWithManifest(innerException, session);
            });
            if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index))
            {
                var wrapper = new KnownTypeObjectSerializer(exceptionSerializer, index);
                typeMapping.TryAdd(type, wrapper);
            }
            else
            {
                typeMapping.TryAdd(type, exceptionSerializer);
            }
            return(exceptionSerializer);
        }
コード例 #20
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var ser = new ObjectSerializer(type);

            if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index))
            {
                var wrapper = new KnownTypeObjectSerializer(ser, index);
                typeMapping.TryAdd(type, wrapper);
            }
            else
            {
                typeMapping.TryAdd(type, ser);
            }
            var          elementSerializer        = serializer.GetSerializerByType(typeof(DictionaryEntry));
            var          preserveObjectReferences = serializer.Options.PreserveObjectReferences;
            ObjectReader reader = (stream, session) =>
            {
                var count    = stream.ReadInt32(session);
                var instance = (IDictionary)Activator.CreateInstance(type, count);
                if (preserveObjectReferences)
                {
                    session.TrackDeserializedObject(instance);
                }

                for (var i = 0; i < count; i++)
                {
                    var entry = (DictionaryEntry)stream.ReadObject(session);
                    instance.Add(entry.Key, entry.Value);
                }
                return(instance);
            };

            ObjectWriter writer = (stream, obj, session) =>
            {
                if (preserveObjectReferences)
                {
                    session.TrackSerializedObject(obj);
                }
                var dict = obj as IDictionary;
                // ReSharper disable once PossibleNullReferenceException
                Int32Serializer.WriteValueImpl(stream, dict.Count, session);
                foreach (DictionaryEntry item in dict)
                {
                    stream.WriteObject(item, typeof(DictionaryEntry), elementSerializer,
                                       serializer.Options.PreserveObjectReferences, session);
                    // elementSerializer.WriteValue(stream,item,session);
                }
            };

            ser.Initialize(reader, writer);

            return(ser);
        }
コード例 #21
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var preserveObjectReferences = serializer.Options.PreserveObjectReferences;
            var ser = new ObjectSerializer(type);

            typeMapping.TryAdd(type, ser);
            var elementSerializer = serializer.GetSerializerByType(typeof(DictionaryEntry));

            object Reader(Stream stream, DeserializerSession session)
            {
                var instance = (IDictionary <string, object>)Activator.CreateInstance(type) !;

                if (preserveObjectReferences)
                {
                    session.TrackDeserializedObject(instance);
                }
                var count = stream.ReadInt32(session);

                for (var i = 0; i < count; i++)
                {
                    var entry = (KeyValuePair <string, object>)stream.ReadObject(session);
                    instance.Add(entry);
                }

                return(instance);
            }

            void Writer(Stream stream, object obj, SerializerSession session)
            {
                if (preserveObjectReferences)
                {
                    session.TrackSerializedObject(obj);
                }
                var dict = (IDictionary <string, object>)obj;

                // ReSharper disable once PossibleNullReferenceException
                Int32Serializer.WriteValueImpl(stream, dict.Count, session);
                foreach (var item in dict)
                {
                    stream.WriteObject(item, typeof(DictionaryEntry), elementSerializer,
                                       serializer.Options.PreserveObjectReferences, session);
                }
                // elementSerializer.WriteValue(stream,item,session);
            }

            ser.Initialize(Reader, Writer);

            return(ser);
        }
コード例 #22
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var serializableSerializer = new ObjectSerializer(type);

            typeMapping.TryAdd(type, serializableSerializer);

            object Reader(Stream stream, DeserializerSession session)
            {
                var dict = stream.ReadObject(session) as Dictionary <string, object>;
                var info = new SerializationInfo(type, new FormatterConverter());

                // ReSharper disable once PossibleNullReferenceException
                foreach (var item in dict)
                {
                    info.AddValue(item.Key, item.Value);
                }

                var ctor = type.GetConstructor(BindingFlagsEx.All, null,
                                               new[] { typeof(SerializationInfo), typeof(StreamingContext) }, null) !;
                var instance = ctor.Invoke(new object[] { info, new StreamingContext() });
                var deserializationCallback = instance as IDeserializationCallback;

                deserializationCallback?.OnDeserialization(this);
                return(instance);
            }

            void Writer(Stream stream, object o, SerializerSession session)
            {
                var info         = new SerializationInfo(type, new FormatterConverter());
                var serializable = o as ISerializable;

                // ReSharper disable once PossibleNullReferenceException
                serializable !.GetObjectData(info, new StreamingContext());
                var dict = new Dictionary <string, object>();

                foreach (var item in info)
                {
                    dict.Add(item.Name, item !.Value !);
                }
                stream.WriteObjectWithManifest(dict, session);
            }

            serializableSerializer.Initialize(Reader, Writer);

            return(serializableSerializer);
        }
コード例 #23
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var os = new ObjectSerializer(type);

            typeMapping.TryAdd(type, os);
            ObjectReader reader = (stream, session) =>
            {
                var name      = stream.ReadString(session);
                var owner     = stream.ReadObject(session) as Type;
                var arguments = stream.ReadObject(session) as Type[];

#if NET45
                var method = owner.GetTypeInfo().GetMethod(
                    name,
                    BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                    null,
                    CallingConventions.Any,
                    arguments,
                    null);
                return(method);
#else
                var methods = owner.GetTypeInfo()
                              .GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public |
                                          BindingFlags.NonPublic);
                var method = methods.FirstOrDefault(m => m.Name == name &&
                                                    m.GetParameters()
                                                    .Select(p => p.ParameterType)
                                                    .SequenceEqual(arguments));
                return(method);
#endif
            };
            ObjectWriter writer = (stream, obj, session) =>
            {
                var method    = (MethodInfo)obj;
                var name      = method.Name;
                var owner     = method.DeclaringType;
                var arguments = method.GetParameters().Select(p => p.ParameterType).ToArray();
                StringSerializer.WriteValueImpl(stream, name, session);
                stream.WriteObjectWithManifest(owner, session);
                stream.WriteObjectWithManifest(arguments, session);
            };

            os.Initialize(reader, writer);

            return(os);
        }
コード例 #24
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var serializableSerializer = new ObjectSerializer(type);

            typeMapping.TryAdd(type, serializableSerializer);
            ValueReader reader = (stream, session) =>
            {
                var dict = stream.ReadObject(session) as Dictionary <string, object>;
                var info = new SerializationInfo(type, new FormatterConverter());
                // ReSharper disable once PossibleNullReferenceException
                foreach (var item in dict)
                {
                    info.AddValue(item.Key, item.Value);
                }

                // protected Dictionary(SerializationInfo info, StreamingContext context);
                var flags = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Default;
                var ctor  = type.GetConstructor(flags, null,
                                                new[] { typeof(SerializationInfo), typeof(StreamingContext) }, null);
                var instance = ctor.Invoke(new object[] { info, new StreamingContext() });
                var deserializationCallback = instance as IDeserializationCallback;
                deserializationCallback?.OnDeserialization(this);
                return(instance);
            };

            ValueWriter writer = (stream, o, session) =>
            {
                var info         = new SerializationInfo(type, new FormatterConverter());
                var serializable = o as ISerializable;
                // ReSharper disable once PossibleNullReferenceException
                serializable.GetObjectData(info, new StreamingContext());
                var dict = new Dictionary <string, object>();
                foreach (var item in info)
                {
                    dict.Add(item.Name, item.Value);
                }
                var dictSerializer = serializer.GetSerializerByType(typeof(Dictionary <string, object>));
                stream.WriteObject(dict, typeof(Dictionary <string, object>), dictSerializer,
                                   serializer.Options.PreserveObjectReferences, session);
            };

            serializableSerializer.Initialize(reader, writer);


            return(serializableSerializer);
        }
コード例 #25
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var x = new ObjectSerializer(type);

            typeMapping.TryAdd(type, x);

            var keyType   = GetKeyType(type);
            var valueType = GetValyeType(type);
            var tupleType = typeof(Tuple <,>).MakeGenericType(keyType, valueType);
            var arrType   = tupleType.MakeArrayType();

            var mapModule       = type.GetTypeInfo().Assembly.GetType("Microsoft.FSharp.Collections.MapModule");
            var ofArray         = mapModule.GetTypeInfo().GetMethod("OfArray");
            var ofArrayConcrete = ofArray.MakeGenericMethod(keyType, valueType);
            var ofArrayCompiled = CompileToDelegate(ofArrayConcrete, arrType);

            var toArray         = mapModule.GetTypeInfo().GetMethod("ToArray");
            var toArrayConcrete = toArray.MakeGenericMethod(keyType, valueType);
            var toArrayCompiled = CompileToDelegate(toArrayConcrete, type);

            var arrSerializer            = serializer.GetSerializerByType(arrType);
            var preserveObjectReferences = serializer.Options.PreserveObjectReferences;

            void Writer(Stream stream, object o, SerializerSession session)
            {
                var arr = toArrayCompiled(o);

                arrSerializer.WriteValue(stream, arr, session);
                if (preserveObjectReferences)
                {
                    session.TrackSerializedObject(o);
                }
            }

            object Reader(Stream stream, DeserializerSession session)
            {
                var arr = arrSerializer.ReadValue(stream, session);
                var res = ofArrayCompiled(arr);

                return(res);
            }

            x.Initialize(Reader, Writer);
            return(x);
        }
コード例 #26
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var arraySerializer = new ObjectSerializer(type);

            var elementType              = type.GetElementType();
            var elementSerializer        = serializer.GetSerializerByType(elementType);
            var preserveObjectReferences = serializer.Options.PreserveObjectReferences;
            //TODO: code gen this part
            ObjectReader reader = (stream, session) =>
            {
                var length = stream.ReadInt32(session);
                var array  = Array.CreateInstance(elementType, length); //create the array
                if (preserveObjectReferences)
                {
                    session.TrackDeserializedObject(array);
                }

                ReadValues(length, stream, session, (dynamic)array);

                return(array);
            };
            ObjectWriter writer = (stream, arr, session) =>
            {
                if (preserveObjectReferences)
                {
                    session.TrackSerializedObject(arr);
                }

                WriteValues((dynamic)arr, stream, elementType, elementSerializer, session);
            };

            arraySerializer.Initialize(reader, writer);

            if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index))
            {
                var wrapper = new KnownTypeObjectSerializer(arraySerializer, index);
                typeMapping.TryAdd(type, wrapper);
            }
            else
            {
                typeMapping.TryAdd(type, arraySerializer);
            }
            return(arraySerializer);
        }
コード例 #27
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        CachedReadConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var os = new ObjectSerializer(type);

            typeMapping.TryAdd(type, os);
            ObjectReader reader = (stream, session) =>
            {
                var name           = stream.ReadString(session);
                var owner          = stream.ReadObject(session) as Type;
                var parameterTypes = stream.ReadObject(session) as Type[];
                var method         = owner.GetMethodExt(name,
                                                        BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                                                        parameterTypes);
                if (method.IsGenericMethodDefinition)
                {
                    var genericTypeArguments = stream.ReadObject(session) as Type[];
                    method = method.MakeGenericMethod(genericTypeArguments);
                }

                return(method);
            };
            ObjectWriter writer = (stream, obj, session) =>
            {
                var method = (MethodInfo)obj;
                var name   = method.Name;
                var owner  = method.DeclaringType;

                StringSerializer.WriteValueImpl(stream, name, session);
                stream.WriteObjectWithManifest(owner, session);
                var arguments = method.GetParameters().Select(p => p.ParameterType).ToArray();
                stream.WriteObjectWithManifest(arguments, session);
                if (method.IsGenericMethod)
                {
                    // we use the parameter types to find the method above but, if generic, we need to store the generic type arguments as well
                    // in order to MakeGenericType
                    var genericTypeArguments = method.GetGenericArguments();
                    stream.WriteObjectWithManifest(genericTypeArguments, session);
                }
            };

            os.Initialize(reader, writer);

            return(os);
        }
コード例 #28
0
            public override ValueSerializer BuildSerializer(Serializer serializer, Type type, ConcurrentDictionary <Type, ValueSerializer> typeMapping)
            {
                var os = new ObjectSerializer(type);

                typeMapping.TryAdd(type, os);
                ObjectReader reader = (stream, session) =>
                {
                    var raw = stream.ReadString(session);
                    return(new DerivedContainer());
                };
                ObjectWriter writer = (stream, value, session) =>
                {
                    StringSerializer.WriteValueImpl(stream, "test", session);
                };

                os.Initialize(reader, writer);
                return(os);
            }
コード例 #29
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type, ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var enumSerializer = new ObjectSerializer(type);

            object Reader(Stream stream, DeserializerSession session)
            {
                var intValue = Int32Serializer.Instance.ReadValue(stream, session);

                return(Enum.ToObject(type, intValue));
            }

            void Writer(Stream stream, object enumValue, SerializerSession session)
            {
                Int32Serializer.Instance.WriteValue(stream, enumValue, session);
            }

            enumSerializer.Initialize(Reader, Writer);
            typeMapping.TryAdd(type, enumSerializer);
            return(enumSerializer);
        }
コード例 #30
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var arraySerializer   = new ObjectSerializer(type);
            var elementType       = type.GetElementType();
            var elementSerializer = serializer.GetSerializerByType(elementType);

            //TODO: code gen this part
            arraySerializer.Initialize((stream, session) =>
            {
                var length = stream.ReadInt32(session);
                var array  = Array.CreateInstance(elementType, length); //create the array

                return(ReadValues(length, stream, session, (dynamic)array));
            }, (stream, arr, session) =>
            {
                WriteValues((dynamic)arr, stream, elementType, elementSerializer, session);
            });
            typeMapping.TryAdd(type, arraySerializer);
            return(arraySerializer);
        }