Exemplo n.º 1
0
 public static byte[] Serializer(object obj)
 {
     using (SerializerInputStream serializerInputStream = new SerializerInputStream())
     {
         Serializer(obj, serializerInputStream);
         return(serializerInputStream.ToBytes());
     }
 }
Exemplo n.º 2
0
 internal static void Serializer(object obj, SerializerInputStream serializerInputStream)
 {
     if (obj == null)
     {
         new ObjectSerializer(null).WriteBytes(obj, serializerInputStream);
     }
     else
     {
         var type = obj.GetType();
         if (type.IsGenericType && TypeUtil.NullableType.IsAssignableFrom(type.GetGenericTypeDefinition()))
         {
             type = type.GetGenericArguments()[0];
         }
         var typeHandle = type.TypeHandle;
         if (RpcSerializerUtil.SerializerMap.ContainsKey(typeHandle))
         {
             RpcSerializerUtil.SerializerMap[typeHandle].WriteBytes(obj, serializerInputStream);
         }
         else
         {
             if (type.IsEnum)
             {
                 new EnumSerializer(type).WriteBytes(obj, serializerInputStream);
             }
             else if (type.IsArray)
             {
                 new ArraySerializercs(type).WriteBytes(obj, serializerInputStream);
             }
             else if (TypeUtil.IDictionaryType.IsAssignableFrom(type))
             {
                 new DictionarySerializer(type).WriteBytes(obj, serializerInputStream);
             }
             else if (TypeUtil.IEnumerableType.IsAssignableFrom(type) || type.DeclaringType == typeof(Enumerable))
             {
                 new EnumerableSerializer(type).WriteBytes(obj, serializerInputStream);
             }
             else
             {
                 new ObjectSerializer(type).WriteBytes(obj, serializerInputStream);
             }
         }
     }
 }