Пример #1
0
        public static T Deserialize <T>(Stream stream) where T : class
        {
            T result;

            try
            {
                if (DxSerializationUtil.UseBinarySerialize)
                {
                    result = (T)((object)DxBinarySerializationUtil.Deserialize(stream));
                }
                else
                {
                    DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(T), null, int.MaxValue, false, false, null, new DxSerializationUtil.SharedTypeResolver());
                    result = (T)((object)dataContractSerializer.ReadObject(stream));
                }
            }
            catch (Exception ex)
            {
                EventLogger.LogErr("Deserialize<T> err: {0}", new object[]
                {
                    ex
                });
                throw new DxStoreSerializeException(ex.Message, ex);
            }
            return(result);
        }
Пример #2
0
 private static TypedSerializationFormatter.TypeBinder ConstructBinder()
 {
     TypedSerializationFormatter.TypeBinder result = null;
     try
     {
         int    tickCount              = Environment.TickCount;
         Type[] serializableTypes      = DxBinarySerializationUtil.GetSerializableTypes();
         Type[] genericTypeDefinitions = DxBinarySerializationUtil.GetGenericTypeDefinitions(serializableTypes);
         result = new TypedSerializationFormatter.TypeBinder(serializableTypes, DxBinarySerializationUtil.safeBaseClasses, genericTypeDefinitions, new TypedSerializationFormatter.TypeEncounteredDelegate(DxBinarySerializationUtil.WriteTypeEvent), true);
         DxBinarySerializationUtil.LogErr("SerializationTypeBinder create TypeBinder succeeded in {0} ms with {1} serializable types and {2} generic types.", new object[]
         {
             Environment.TickCount - tickCount,
             serializableTypes.Length,
             genericTypeDefinitions.Length
         });
     }
     catch (Exception ex)
     {
         DxBinarySerializationUtil.LogErr("SerializationTypeBinder create TypeBinder failed: {0}.", new object[]
         {
             ex.ToString()
         });
     }
     return(result);
 }
Пример #3
0
        public static MemoryStream Serialize <T>(T obj) where T : class
        {
            MemoryStream result;

            try
            {
                MemoryStream memoryStream = new MemoryStream();
                if (DxSerializationUtil.UseBinarySerialize)
                {
                    DxBinarySerializationUtil.Serialize(memoryStream, obj);
                }
                else
                {
                    DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(T), null, int.MaxValue, false, false, null, new DxSerializationUtil.SharedTypeResolver());
                    dataContractSerializer.WriteObject(memoryStream, obj);
                }
                result = memoryStream;
            }
            catch (Exception ex)
            {
                EventLogger.LogErr("Serialize<T> err: {0}", new object[]
                {
                    ex
                });
                throw new DxStoreSerializeException(ex.Message, ex);
            }
            return(result);
        }
Пример #4
0
 private static TypedSerializationFormatter.TypeBinder GetTypeBinder()
 {
     if (DxBinarySerializationUtil.staticBinder == null)
     {
         lock (DxBinarySerializationUtil.binderLock)
         {
             if (DxBinarySerializationUtil.staticBinder == null)
             {
                 DxBinarySerializationUtil.staticBinder = DxBinarySerializationUtil.ConstructBinder();
             }
         }
     }
     return(DxBinarySerializationUtil.staticBinder);
 }
Пример #5
0
        private static void WriteTypeEvent(Type type, bool allowed)
        {
            if (allowed)
            {
                return;
            }
            StringBuilder stringBuilder = new StringBuilder("Need to add type ");

            if (type == null)
            {
                stringBuilder.AppendLine("no types. how does that make sense?");
            }
            else
            {
                stringBuilder.AppendLine(type.FullName);
            }
            stringBuilder.AppendLine("here's the stack\n");
            stringBuilder.AppendLine(new StackTrace(true).ToString());
            DxBinarySerializationUtil.LogErr(stringBuilder.ToString(), new object[0]);
        }
Пример #6
0
        private static Type[] GetSerializableTypes()
        {
            HashSet <Type> hashSet = new HashSet <Type>();

            try
            {
                foreach (Type type in DxBinarySerializationUtil.expectedTypes)
                {
                    DxBinarySerializationUtil.ExpandSerializableTypes(hashSet, type);
                }
            }
            catch (Exception ex)
            {
                DxBinarySerializationUtil.LogErr("SerializationTypeBinder initialize type in current appdomain failed: {0}.", new object[]
                {
                    ex.ToString()
                });
            }
            return(hashSet.ToArray <Type>());
        }
Пример #7
0
        private static Type[] GetGenericTypeDefinitions(Type[] serializableTypes)
        {
            HashSet <Type> hashSet = new HashSet <Type>();

            foreach (string typeName in DxBinarySerializationUtil.builtinGenericTypes)
            {
                Type type = Type.GetType(typeName);
                if (type != null)
                {
                    hashSet.Add(Type.GetType(typeName));
                }
            }
            foreach (Type type2 in serializableTypes)
            {
                try
                {
                    if (type2.IsGenericTypeDefinition && !hashSet.Contains(type2))
                    {
                        hashSet.Add(type2);
                    }
                    else if (type2.IsConstructedGenericType)
                    {
                        Type genericTypeDefinition = type2.GetGenericTypeDefinition();
                        if (genericTypeDefinition != null && !hashSet.Contains(genericTypeDefinition))
                        {
                            hashSet.Add(genericTypeDefinition);
                        }
                    }
                }
                catch (Exception ex)
                {
                    DxBinarySerializationUtil.LogErr("SerializationTypeBinder GetGenericTypeDefinitions on type {0} failed: {1}.", new object[]
                    {
                        type2.FullName,
                        ex.ToString()
                    });
                }
            }
            return(hashSet.ToArray <Type>());
        }
Пример #8
0
 public static object DeserializeSafe(Stream s)
 {
     return(TypedBinaryFormatter.DeserializeObject(s, DxBinarySerializationUtil.GetTypeBinder()));
 }
Пример #9
0
 public static object Deserialize(Stream s)
 {
     return(DxBinarySerializationUtil.DeserializeSafe(s));
 }
Пример #10
0
 private static void ExpandSerializableTypes(HashSet <Type> serializableTypes, Type type)
 {
     if (serializableTypes.Contains(type))
     {
         return;
     }
     serializableTypes.Add(type);
     try
     {
         if (type.IsArray)
         {
             DxBinarySerializationUtil.ExpandSerializableTypes(serializableTypes, type.GetElementType());
         }
     }
     catch (Exception ex)
     {
         DxBinarySerializationUtil.LogErr("SerializationTypeBinder load array element type on type {0} failed: {1}.", new object[]
         {
             type.FullName,
             ex.ToString()
         });
     }
     try
     {
         if (type.IsConstructedGenericType)
         {
             foreach (Type type2 in type.GetGenericArguments())
             {
                 DxBinarySerializationUtil.ExpandSerializableTypes(serializableTypes, type2);
             }
         }
     }
     catch (Exception ex2)
     {
         DxBinarySerializationUtil.LogErr("SerializationTypeBinder load generic type definition on type {0} failed: {1}.", new object[]
         {
             type.FullName,
             ex2.ToString()
         });
     }
     try
     {
         FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
         if (fields != null && fields.Length > 0)
         {
             foreach (FieldInfo fieldInfo in from x in fields
                      where !x.IsNotSerialized && !serializableTypes.Contains(x.FieldType) && x.FieldType.IsSerializable
                      select x)
             {
                 try
                 {
                     DxBinarySerializationUtil.ExpandSerializableTypes(serializableTypes, fieldInfo.FieldType);
                 }
                 catch (Exception ex3)
                 {
                     DxBinarySerializationUtil.LogErr("SerializationTypeBinder load field {0} on type {1} failed: {2}.", new object[]
                     {
                         fieldInfo.Name,
                         type.FullName,
                         ex3.ToString()
                     });
                 }
             }
         }
     }
     catch (Exception ex4)
     {
         DxBinarySerializationUtil.LogErr("SerializationTypeBinder load fields on type {0} failed: {1}.", new object[]
         {
             type.FullName,
             ex4.ToString()
         });
     }
     try
     {
         PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
         if (properties != null && properties.Length > 0)
         {
             foreach (PropertyInfo propertyInfo in properties)
             {
                 try
                 {
                     DxBinarySerializationUtil.ExpandSerializableTypes(serializableTypes, propertyInfo.PropertyType);
                 }
                 catch (Exception ex5)
                 {
                     DxBinarySerializationUtil.LogErr("SerializationTypeBinder load property {0} on type {1} failed: {2}.", new object[]
                     {
                         propertyInfo.Name,
                         type.FullName,
                         ex5.ToString()
                     });
                 }
             }
         }
     }
     catch (Exception ex6)
     {
         DxBinarySerializationUtil.LogErr("SerializationTypeBinder load properties on type {0} failed: {1}.", new object[]
         {
             type.FullName,
             ex6.ToString()
         });
     }
     try
     {
         if (type.BaseType != null && type.BaseType != typeof(object))
         {
             DxBinarySerializationUtil.ExpandSerializableTypes(serializableTypes, type.BaseType);
         }
     }
     catch (Exception ex7)
     {
         DxBinarySerializationUtil.LogErr("SerializationTypeBinder load base type on type {0} failed: {1}.", new object[]
         {
             type.FullName,
             ex7.ToString()
         });
     }
 }