예제 #1
0
파일: NetworkBinary.cs 프로젝트: bmjoy/SNet
        /// <summary>
        /// Serialize an object into a byte array
        /// </summary>
        /// <param name="obj">The object to serialize</param>
        /// <typeparam name="T">The type of the object</typeparam>
        /// <returns>A byte array of the serialized object</returns>
        public static byte[] Serialize <T>(T obj)
        {
            var t = typeof(T);

            if (t.IsPrimitive)
            {
                return(NetworkPrimitiveSerializer.Serialize(obj));
            }
            if (t == typeof(string))
            {
                return(NetworkStringSerializer.Serialize((string)(object)obj));
            }
            if (t.IsEnum)
            {
                return(NetworkEnumSerializer.Serialize(obj, typeof(byte)));
            }
            return(NetworkClassSerializer.Serialize(obj));
        }
예제 #2
0
 /// <summary>
 /// Serialize an object into a byte array
 /// </summary>
 /// <param name="obj">The object to serialize</param>
 /// <param name="cast">The type of the enum</param>
 /// <typeparam name="T">The type of the object</typeparam>
 /// <returns>A byte array of the serialized object</returns>
 public static byte[] Serialize <T>(T obj, Type cast)
 {
     return(NetworkPrimitiveSerializer.Serialize(Convert.ChangeType(obj, cast)));
 }