1. C# / Говнокод #5504

    +116

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    public class TObjects
    	{
    		public delegate T DeferredConstruction<out T>();
    
    		public static void Dispose<T>(ref T objectForDispose)
    		{
    			var typeOfObjectForDispose = typeof(T);
    			if (!typeOfObjectForDispose.IsClass)
    				DisposeIfIDisposable(ref objectForDispose, typeOfObjectForDispose);
    			else
    				if (!Equals(objectForDispose, null))
    					DisposeIfIDisposable(ref objectForDispose, typeOfObjectForDispose);
    			objectForDispose = default(T);
    			//objectForDispose = (T)(object)(null);
    		}
    
    		public static void Create<T>(ref T objectForCreate, DeferredConstruction<T> newObject)
    		{
    			Dispose(ref objectForCreate);
    			objectForCreate = newObject();
    		}
      private static void DisposeIfIDisposable<T>(ref T objectForDispose, Type typeOfObjectForDispose)
            {
                bool canDisposable = (objectForDispose as IDisposable) != null;
                if (canDisposable)
                {
                    var dispose = typeOfObjectForDispose.GetMethod("Dispose");
                    dispose.Invoke(objectForDispose, new object[] { });
                }
            }
    }

    Запостил: Говногость, 03 Февраля 2011

    Комментарии (5) RSS

    • Я так и не понял зачем, но в этом же классе также есть методы:
      public static void DisposeStruct<T>(ref T objectForDispose) where T : struct, IDisposable
      		{
      			objectForDispose.Dispose();
      		}
      
      		public static void DisposeClass<T>(ref T objectForDispose) where T: class, IDisposable
      		{
      			if (objectForDispose != null)
      				objectForDispose.Dispose();
      			objectForDispose = null;
      		}
      
      		public static void DisposeCreateClass<T>(ref T objectForCreate, DeferredConstruction<T> newObject) where T : class, IDisposable
      		{
      			DisposeClass(ref objectForCreate);
      			objectForCreate = newObject();
      		}
      
      		public static void DisposeCreateStruct<T>(ref T objectForCreate, DeferredConstruction<T> newObject) where T : struct, IDisposable
      		{
      			DisposeStruct(ref objectForCreate);
      			objectForCreate = newObject();
      		}
      
      		public static void FreeClass<T>(ref T objectForDispose) where T : class
      		{
      			objectForDispose = null;
      		}
      
      		public static void FreeStruct<T>(ref T objectForDispose) where T : class
      		{
      			objectForDispose = default(T);
      		}
      
      		public static void CreateClass<T>(ref T objectForCreate, DeferredConstruction<T> newObject) where T : class
      		{
      			objectForCreate = newObject();
      		}
      
      		public static void CreateStruct<T>(ref T objectForCreate, DeferredConstruction<T> newObject) where T : struct
      		{
      			objectForCreate = newObject();
      		}
      Ответить

    Добавить комментарий