示例#1
0
        protected void RegisterTypes(RuntimeTypeModel model)
        {
            Type[] serializableTypes = Reflection.GetAllFromCurrentAssembly()
                                       .Where(type => type.IsDefined(typeof(ProtoContractAttribute), false)).ToArray();

            foreach (Type type in serializableTypes)
            {
                if (type.IsGenericType())
                {
                    continue;
                }

                model.Add(type, true);
                model.Add(typeof(PrimitiveContract <>).MakeGenericType(type.MakeArrayType()), true);
                model.Add(typeof(List <>).MakeGenericType(type), true);
            }


#if !UNITY_WINRT
            Dictionary <System.Type, ISerializationSurrogate> surrogates = SerializationSurrogates.GetSurrogates();
            foreach (KeyValuePair <System.Type, ISerializationSurrogate> surrogate in surrogates)
#else
            Dictionary <System.Type, object> surrogates = SerializationSurrogates.GetSurrogates();
            foreach (KeyValuePair <System.Type, object> surrogate in surrogates)
#endif
            {
                model.Add(surrogate.Value.GetType(), true);
                model.Add(surrogate.Key, false).SetSurrogate(surrogate.Value.GetType());

                model.Add(typeof(PrimitiveContract <>).MakeGenericType(surrogate.Key.MakeArrayType()), true);
                model.Add(typeof(List <>).MakeGenericType(surrogate.Key), true);
            }
        }
示例#2
0
 public static TData Deserialize <TData>(byte[] b)
 {
     using (var stream = new MemoryStream(b))
     {
         var formatter = new BinaryFormatter();
         // formatter.Binder = new Binder();
         formatter.SurrogateSelector = SerializationSurrogates.CreateSelector();
         stream.Seek(0, SeekOrigin.Begin);
         object deserialized = formatter.Deserialize(stream);
         return((TData)deserialized);
     }
 }
示例#3
0
 public static byte[] Serialize <TData>(TData settings)
 {
     using (var stream = new MemoryStream())
     {
         var formatter = new BinaryFormatter();
         // formatter.Binder = new Binder();
         formatter.SurrogateSelector = SerializationSurrogates.CreateSelector();
         formatter.Serialize(stream, settings);
         stream.Flush();
         stream.Position = 0;
         return(stream.ToArray());
     }
 }