예제 #1
0
        /// <summary>
        /// Serialize an object
        /// </summary>
        /// <typeparam name="T">Any reference type</typeparam>
        /// <param name="obj">Object to serialize</param>
        /// <param name="options">Serialization options</param>
        /// <returns>Byte array containing the serialized object</returns>
        public static byte[] Serialize <T>(T obj, SerializationOptions options)
        {
            using (var ms = new MemoryStream())
            {
                if (obj == null)
                {
                    return(new byte[0]);
                }

                object objToSerialize = ObjectCleaner.PrepareObjectForSerialization(obj);
                var    cargo          = new SerializerObjectTracker();


                var serializerMethod       = GetTypeToObjectSerializer(obj.GetType());
                var stringSerializerMethod = (Action <Stream, string, SerializerObjectTracker>)GetTypeSerializer(typeof(string));
                var intSerializerMethod    = (Action <Stream, int, SerializerObjectTracker>)GetTypeSerializer(typeof(int));

                WriteHeader(ms, cargo, options);

                if (!options.OmitRootTypeName)
                {
                    stringSerializerMethod(ms, SerializedTypeResolver.GetShortNameFromType(obj.GetType()), cargo);
                }

                intSerializerMethod(ms, SerializedTypeResolver.GetHashCodeFromType(obj.GetType()), cargo);
                serializerMethod(ms, objToSerialize, cargo);

                return(ms.ToArray());
            }
        }
 public ObjectPool(int capacity, ObjectConstructor creator, ObjectCleaner cleaner = null)
 {
     mConstructor = creator;
     mCleaner     = cleaner;
     mCache       = new T[Mathf.Max(32, capacity)];
     mLen         = 0;
 }
예제 #3
0
 public ObjectBuffer(int initCapacity = 10, ObjectGenerater <T> creater = null, ObjectCleaner <T> destroier = null)
 {
     mBuffer   = new T[initCapacity];
     mLen      = 0;
     mLock     = new object();
     Creater   = creater;
     Destroier = destroier;
 }
 public void SetCleaner(ObjectCleaner cleaner)
 {
     mCleaner = cleaner;
     if (mCleaner == null && mWorker != null)
     {
         mCleaner = mWorker.CleanData;
     }
 }
예제 #5
0
 public ObjectPool(int groups, int initCapacity = 10, ObjectGenerater <int, T> creater = null, ObjectCleaner <int, T> destroier = null)
 {
     mBuffer = new T[groups][];
     mLen    = new int[groups];
     for (int i = 0; i < groups; i++)
     {
         mBuffer[i] = new T[initCapacity];
         mLen[i]    = 0;
     }
     mLock     = new object();
     Creater   = creater;
     Destroier = destroier;
 }
예제 #6
0
        /// <summary>
        /// Clone an object
        /// </summary>
        /// <typeparam name="T">Any reference type</typeparam>
        /// <param name="obj">Object to clone</param>
        /// <returns>The cloned object</returns>
        public static T Clone <T>(T obj)
        {
            if (obj == null)
            {
                return(default(T));
            }

            object objToClone = ObjectCleaner.PrepareObjectForSerialization(obj);

            var refTracker = new ObjectClonerReferenceTracker();
            var clone      = CloneImpl(objToClone.GetType())(objToClone, refTracker);

            var queryable = IQueryableCloner.GetInterfaceType(obj.GetType(), typeof(IQueryable <>));

            if (queryable != null)
            {
                var genericArgumentType = queryable.GetGenericArguments()[0];
                return((T)Deserializer.ConvertObjectToIQueryable(clone, typeof(IQueryable <>).MakeGenericType(genericArgumentType)));
            }

            return((T)clone);
        }