/// <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; }
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; } }
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; }
/// <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); }