Ejemplo n.º 1
0
    public void DisposableObjectsAreDisposedWhenThrowingAnException()
    {
        var disposableResource = new DisposableResource();

        Assert.Throws <Exception>(() => ErrorHandling.DisposableResourcesAreDisposedWhenExceptionIsThrown(disposableResource));
        Assert.True(disposableResource.IsDisposed);
    }
Ejemplo n.º 2
0
    public void ShouldDisposeResource()
    {
        var    resource = new DisposableResource();
        Action a        = () => resource.Do();

        //Classic using statement to illustrate that the Dispose() method is called after the using block
        using (resource){ a(); }
        Assert.Throws <ObjectDisposedException>(a);
    }
Ejemplo n.º 3
0
    public void ShouldDisposeResourceAfterUsingFunction()
    {
        var resource = new DisposableResource();
        Action <DisposableResource> a = (r) => resource.Do();
        //An extension method which turns an Action<T> into a Func<T, R>
        Func <DisposableResource, Unit> f = a.ToFunc();

        //Now we can use our Using HOF as intended
        Using(resource, f);

        //The resource is disposed and should throw an ObjectDisposedException
        Assert.Throws <ObjectDisposedException>(() => resource.Do());
    }
 public Derived()
 {
     try
     {
         //				throw new Exception();		// Exception 3.
         _derivedCtorInit = new DisposableResource("_derivedCtorInit");
         //				throw new Exception();
     }
     catch (Exception)
     {
         //				Dispose();					// Uncomment to perform cleanup.
         throw;
     }
 }
 public Base()
 {
     try
     {
         throw new Exception();                                      // Exception 1.
         _baseCtorInit = new DisposableResource("_baseCtorInit");
         //				throw new Exception();		// Exception 2.
     }
     catch (Exception)
     {
         //				Dispose();					// Uncomment to perform cleanup.
         throw;
     }
 }
Ejemplo n.º 6
0
 public static void CreateAndUse()
 {
     using var resource = new DisposableResource();
     resource.Use();
 }