IDisposable 인터페이스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
관리되지 않은 리소스 해제를 위한 메커니즘을 제공합니다.
public interface class IDisposable
public interface IDisposable
[System.Runtime.InteropServices.ComVisible(true)]
public interface IDisposable
type IDisposable = interface
[<System.Runtime.InteropServices.ComVisible(true)>]
type IDisposable = interface
Public Interface IDisposable
- 파생
- 특성
예제
다음 예제에서는 인터페이스를 구현하는 리소스 클래스를 만드는 방법을 보여 줍니다 IDisposable .
#using <System.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
// The following example demonstrates how to create a class that
// implements the IDisposable interface and the IDisposable.Dispose
// method with finalization to clean up unmanaged resources.
//
public ref class MyResource: public IDisposable
{
private:
// Pointer to an external unmanaged resource.
IntPtr handle;
// A managed resource this class uses.
Component^ component;
// Track whether Dispose has been called.
bool disposed;
public:
// The class constructor.
MyResource( IntPtr handle, Component^ component )
{
this->handle = handle;
this->component = component;
disposed = false;
}
// This method is called if the user explicitly disposes of the
// object (by calling the Dispose method in other managed languages,
// or the destructor in C++). The compiler emits as a call to
// GC::SuppressFinalize( this ) for you, so there is no need to
// call it here.
~MyResource()
{
// Dispose of managed resources.
component->~Component();
// Call C++ finalizer to clean up unmanaged resources.
this->!MyResource();
// Mark the class as disposed. This flag allows you to throw an
// exception if a disposed object is accessed.
disposed = true;
}
// Use interop to call the method necessary to clean up the
// unmanaged resource.
//
[System::Runtime::InteropServices::DllImport("Kernel32")]
static Boolean CloseHandle( IntPtr handle );
// The C++ finalizer destructor ensures that unmanaged resources get
// released if the user releases the object without explicitly
// disposing of it.
//
!MyResource()
{
// Call the appropriate methods to clean up unmanaged
// resources here. If disposing is false when Dispose(bool,
// disposing) is called, only the following code is executed.
CloseHandle( handle );
handle = IntPtr::Zero;
}
};
void main()
{
// Insert code here to create and use the MyResource object.
MyResource^ mr = gcnew MyResource((IntPtr) 42, (Component^) gcnew Button());
mr->~MyResource();
}
using System;
using System.ComponentModel;
// The following example demonstrates how to create
// a resource class that implements the IDisposable interface
// and the IDisposable.Dispose method.
public class DisposeExample
{
// A base class that implements IDisposable.
// By implementing IDisposable, you are announcing that
// instances of this type allocate scarce resources.
public class MyResource: IDisposable
{
// Pointer to an external unmanaged resource.
private IntPtr handle;
// Other managed resource this class uses.
private Component component = new Component();
// Track whether Dispose has been called.
private bool disposed = false;
// The class constructor.
public MyResource(IntPtr handle)
{
this.handle = handle;
}
// Implement IDisposable.
// Do not make this method virtual.
// A derived class should not be able to override this method.
public void Dispose()
{
Dispose(disposing: true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SuppressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}
// Dispose(bool disposing) executes in two distinct scenarios.
// If disposing equals true, the method has been called directly
// or indirectly by a user's code. Managed and unmanaged resources
// can be disposed.
// If disposing equals false, the method has been called by the
// runtime from inside the finalizer and you should not reference
// other objects. Only unmanaged resources can be disposed.
protected virtual void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if(!this.disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if(disposing)
{
// Dispose managed resources.
component.Dispose();
}
// Call the appropriate methods to clean up
// unmanaged resources here.
// If disposing is false,
// only the following code is executed.
CloseHandle(handle);
handle = IntPtr.Zero;
// Note disposing has been done.
disposed = true;
}
}
// Use interop to call the method necessary
// to clean up the unmanaged resource.
[System.Runtime.InteropServices.DllImport("Kernel32")]
private extern static Boolean CloseHandle(IntPtr handle);
// Use C# finalizer syntax for finalization code.
// This finalizer will run only if the Dispose method
// does not get called.
// It gives your base class the opportunity to finalize.
// Do not provide finalizer in types derived from this class.
~MyResource()
{
// Do not re-create Dispose clean-up code here.
// Calling Dispose(disposing: false) is optimal in terms of
// readability and maintainability.
Dispose(disposing: false);
}
}
public static void Main()
{
// Insert code here to create
// and use the MyResource object.
}
}
// The following example demonstrates how to create
// a resource class that implements the IDisposable interface
// and the IDisposable.Dispose method.
open System
open System.ComponentModel
open System.Runtime.InteropServices
// Use interop to call the method necessary
// to clean up the unmanaged resource.
[<DllImport "Kernel32">]
extern Boolean CloseHandle(nativeint handle)
// A base class that implements IDisposable.
// By implementing IDisposable, you are announcing that
// instances of this type allocate scarce resources.
type MyResource(handle: nativeint) =
// Pointer to an external unmanaged resource.
let mutable handle = handle
// Other managed resource this class uses.
let comp = new Component()
// Track whether Dispose has been called.
let mutable disposed = false
// Implement IDisposable.
// Do not make this method virtual.
// A derived class should not be able to override this method.
interface IDisposable with
member this.Dispose() =
this.Dispose true
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SuppressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize this
// Dispose(bool disposing) executes in two distinct scenarios.
// If disposing equals true, the method has been called directly
// or indirectly by a user's code. Managed and unmanaged resources
// can be disposed.
// If disposing equals false, the method has been called by the
// runtime from inside the finalizer and you should not reference
// other objects. Only unmanaged resources can be disposed.
abstract Dispose: bool -> unit
override _.Dispose(disposing) =
// Check to see if Dispose has already been called.
if not disposed then
// If disposing equals true, dispose all managed
// and unmanaged resources.
if disposing then
// Dispose managed resources.
comp.Dispose()
// Call the appropriate methods to clean up
// unmanaged resources here.
// If disposing is false,
// only the following code is executed.
CloseHandle handle |> ignore
handle <- IntPtr.Zero
// Note disposing has been done.
disposed <- true
// This finalizer will run only if the Dispose method
// does not get called.
// It gives your base class the opportunity to finalize.
// Do not provide finalizer in types derived from this class.
override this.Finalize() =
// Do not re-create Dispose clean-up code here.
// Calling Dispose(disposing: false) is optimal in terms of
// readability and maintainability.
this.Dispose false
Imports System.ComponentModel
' The following example demonstrates how to create
' a resource class that implements the IDisposable interface
' and the IDisposable.Dispose method.
Public Class DisposeExample
' A class that implements IDisposable.
' By implementing IDisposable, you are announcing that
' instances of this type allocate scarce resources.
Public Class MyResource
Implements IDisposable
' Pointer to an external unmanaged resource.
Private handle As IntPtr
' Other managed resource this class uses.
Private component As component
' Track whether Dispose has been called.
Private disposed As Boolean = False
' The class constructor.
Public Sub New(ByVal handle As IntPtr)
Me.handle = handle
End Sub
' Implement IDisposable.
' Do not make this method virtual.
' A derived class should not be able to override this method.
Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose(disposing:=True)
' This object will be cleaned up by the Dispose method.
' Therefore, you should call GC.SupressFinalize to
' take this object off the finalization queue
' and prevent finalization code for this object
' from executing a second time.
GC.SuppressFinalize(Me)
End Sub
' Dispose(bool disposing) executes in two distinct scenarios.
' If disposing equals true, the method has been called directly
' or indirectly by a user's code. Managed and unmanaged resources
' can be disposed.
' If disposing equals false, the method has been called by the
' runtime from inside the finalizer and you should not reference
' other objects. Only unmanaged resources can be disposed.
Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
' Check to see if Dispose has already been called.
If Not Me.disposed Then
' If disposing equals true, dispose all managed
' and unmanaged resources.
If disposing Then
' Dispose managed resources.
component.Dispose()
End If
' Call the appropriate methods to clean up
' unmanaged resources here.
' If disposing is false,
' only the following code is executed.
CloseHandle(handle)
handle = IntPtr.Zero
' Note disposing has been done.
disposed = True
End If
End Sub
' Use interop to call the method necessary
' to clean up the unmanaged resource.
<System.Runtime.InteropServices.DllImport("Kernel32")> _
Private Shared Function CloseHandle(ByVal handle As IntPtr) As [Boolean]
End Function
' This finalizer will run only if the Dispose method
' does not get called.
' It gives your base class the opportunity to finalize.
' Do not provide finalize methods in types derived from this class.
Protected Overrides Sub Finalize()
' Do not re-create Dispose clean-up code here.
' Calling Dispose(disposing:=False) is optimal in terms of
' readability and maintainability.
Dispose(disposing:=False)
MyBase.Finalize()
End Sub
End Class
Public Shared Sub Main()
' Insert code here to create
' and use the MyResource object.
End Sub
End Class
설명
이 인터페이스의 기본 사용은 관리되지 않는 리소스를 해제하는 것입니다. 가비지 수집기는 해당 개체가 더 이상 사용되지 않을 때 관리되는 개체에 할당된 메모리를 자동으로 해제합니다. 그러나 가비지 수집이 발생하는 시기를 예측할 수 없습니다. 또한 가비지 수집기는 창 핸들 또는 파일 및 스트림 열기와 같은 관리되지 않는 리소스에 대한 지식이 없습니다.
이 인터페이스의 메서드를 Dispose 사용하여 가비지 수집기와 함께 관리되지 않는 리소스를 명시적으로 해제합니다. 개체의 소비자가 개체가 더 이상 필요하지 않은 경우 이 메서드를 호출할 수 있습니다.
경고
기존 클래스에 인터페이스를 추가하는 IDisposable 것은 호환성이 손상되는 변경입니다. 형식의 기존 소비자는 를 호출 Dispose할 수 없으므로 형식이 보유한 관리되지 않는 리소스가 릴리스될 것이라고 확신할 수 없습니다.
인스턴스가 IDisposable.Dispose 소유한 리소스가 더 이상 필요하지 않은 경우 형식의 소비자가 구현을 호출하므로 관리되는 개체를 (권장 대안)에 SafeHandle 래핑하거나 소비자가 를 호출Dispose하는 것을 잊어버린 경우 관리되지 않는 리소스를 해제하도록 재정 Object.Finalize 의해야 합니다.
중요
.NET Framework C++ 컴파일러는 리소스의 결정적 삭제를 지원하며 메서드의 직접 구현을 Dispose 허용하지 않습니다.
이 인터페이스와 Object.Finalize 메서드를 사용하는 방법에 대한 자세한 내용은 가비지 수집 및 Dispose 메서드 구현 항목을 참조하세요.
IDisposable을 구현하는 개체 사용
앱에서 인터페이스를 구현 IDisposable 하는 개체만 사용하는 경우 사용이 완료되면 개체의 IDisposable.Dispose 구현을 호출해야 합니다. 프로그래밍 언어에 따라 다음 두 가지 방법 중 하나로 이 작업을 수행할 수 있습니다.
C# 및 Visual Basic의
using
문과 같은 언어 구문과 F#의 문 또는using
함수를use
사용합니다.블록에서 구현
try
/finally
에 대한 호출을 IDisposable.Dispose 래핑합니다.
참고
구현 IDisposable 하는 형식에 대한 설명서는 팩트 및 해당 Dispose 구현을 호출하는 미리 알림을 포함합니다.
C#, F#및 Visual Basic Using 문
언어가 C#의 using 문, Visual Basic의 Using 문 또는 F#의 use 문과 같은 구문을 지원하는 경우 자신을 명시적으로 호출 IDisposable.Dispose 하는 대신 사용할 수 있습니다. 다음 예제에서는 파일에 대한 정보와 파일의 단어 수를 유지하는 클래스를 정의하는 WordCount
데 이 방법을 사용합니다.
using System;
using System.IO;
using System.Text.RegularExpressions;
public class WordCount
{
private String filename = String.Empty;
private int nWords = 0;
private String pattern = @"\b\w+\b";
public WordCount(string filename)
{
if (!File.Exists(filename))
throw new FileNotFoundException("The file does not exist.");
this.filename = filename;
string txt = String.Empty;
using (StreamReader sr = new StreamReader(filename))
{
txt = sr.ReadToEnd();
}
nWords = Regex.Matches(txt, pattern).Count;
}
public string FullName
{ get { return filename; } }
public string Name
{ get { return Path.GetFileName(filename); } }
public int Count
{ get { return nWords; } }
}
open System.IO
open System.Text.RegularExpressions
type WordCount(filename) =
let txt =
if File.Exists filename |> not then
raise (FileNotFoundException "The file does not exist.")
use sr = new StreamReader(filename)
sr.ReadToEnd()
let pattern = @"\b\w+\b"
let nWords = Regex.Matches(txt, pattern).Count
member _.FullName = filename
member _.Name = Path.GetFileName filename
member _.Count = nWords
Imports System.IO
Imports System.Text.RegularExpressions
Public Class WordCount
Private filename As String
Private nWords As Integer
Private pattern As String = "\b\w+\b"
Public Sub New(filename As String)
If Not File.Exists(filename) Then
Throw New FileNotFoundException("The file does not exist.")
End If
Me.filename = filename
Dim txt As String = String.Empty
Using sr As New StreamReader(filename)
txt = sr.ReadToEnd()
End Using
nWords = Regex.Matches(txt, pattern).Count
End Sub
Public ReadOnly Property FullName As String
Get
Return filename
End Get
End Property
Public ReadOnly Property Name As String
Get
Return Path.GetFileName(filename)
End Get
End Property
Public ReadOnly Property Count As Integer
Get
Return nWords
End Get
End Property
End Class
using
문(use
F#의 식)은 실제로 구문 편의성입니다. 컴파일 시 언어 컴파일러는 블록에 대해 IL(중간 언어)을 try
/finally
구현합니다.
문에 using
대한 자세한 내용은 Using 문 또는 using Statement 항목을 참조하세요.
Try/Finally 블록
프로그래밍 언어가 C# 또는 Visual Basic의 using
문이나 F#의 문과 같은 구문을 지원하지 않거나 사용하지 않으려는 경우 문 블록에서 finally
구현을/try
finally
호출 IDisposable.Dispose 할 수 있습니다.use
다음 예제에서는 이전 예제의 using
블록을 블록으로 try
/finally
바꿉니다.
using System;
using System.IO;
using System.Text.RegularExpressions;
public class WordCount2
{
private String filename = String.Empty;
private int nWords = 0;
private String pattern = @"\b\w+\b";
public WordCount2(string filename)
{
if (!File.Exists(filename))
throw new FileNotFoundException("The file does not exist.");
this.filename = filename;
string txt = String.Empty;
StreamReader sr = null;
try
{
sr = new StreamReader(filename);
txt = sr.ReadToEnd();
}
finally
{
if (sr != null) sr.Dispose();
}
nWords = Regex.Matches(txt, pattern).Count;
}
public string FullName
{ get { return filename; } }
public string Name
{ get { return Path.GetFileName(filename); } }
public int Count
{ get { return nWords; } }
}
open System.IO
open System.Text.RegularExpressions
type WordCount2(filename) =
let txt =
if File.Exists filename |> not then
raise (FileNotFoundException "The file does not exist.")
let sr = new StreamReader(filename)
try
sr.ReadToEnd()
finally
sr.Dispose()
let pattern = @"\b\w+\b"
let nWords = Regex.Matches(txt, pattern).Count
member _.FullName = filename
member _.Name = Path.GetFileName filename
member _.Count = nWords
Imports System.IO
Imports System.Text.RegularExpressions
Public Class WordCount2
Private filename As String
Private nWords As Integer
Private pattern As String = "\b\w+\b"
Public Sub New(filename As String)
If Not File.Exists(filename) Then
Throw New FileNotFoundException("The file does not exist.")
End If
Me.filename = filename
Dim txt As String = String.Empty
Dim sr As StreamReader = Nothing
Try
sr = New StreamReader(filename)
txt = sr.ReadToEnd()
Finally
If sr IsNot Nothing Then sr.Dispose()
End Try
nWords = Regex.Matches(txt, pattern).Count
End Sub
Public ReadOnly Property FullName As String
Get
Return filename
End Get
End Property
Public ReadOnly Property Name As String
Get
Return Path.GetFileName(filename)
End Get
End Property
Public ReadOnly Property Count As Integer
Get
Return nWords
End Get
End Property
End Class
패턴에 대한 try
/finally
자세한 내용은 Try...를 참조하세요. 잡을... 마지막으로 문, try-finally, try... finally Expression 또는 try-finally 문입니다.
IDisposable 구현
형식이 관리되지 않는 리소스를 직접 사용하거나 삭제 가능한 리소스를 직접 사용하려는 경우 를 구현 IDisposable 해야 합니다. 인스턴스가 더 이상 필요하지 않은 경우 형식의 소비자가 구현을 호출 IDisposable.Dispose 하여 리소스를 해제할 수 있습니다. 를 호출 Dispose하지 못하는 경우를 처리하려면 에서 SafeHandle 파생된 클래스를 사용하여 관리되지 않는 리소스를 래핑하거나 참조 형식에 대한 메서드를 재정의 Object.Finalize 해야 합니다. 두 경우 모두 메서드를 Dispose 사용하여 관리되지 않는 리소스를 사용한 후 관리되지 않는 리소스 해제, 해제 또는 초기화와 같은 모든 정리를 수행합니다. 구현에 IDisposable.Dispose대한 자세한 내용은 Dispose(bool) 메서드 오버로드를 참조하세요.
중요
관리되지 않는 리소스를 사용하고 삭제해야 하는 하위 클래스가 있거나 있을 가능성이 있는 기본 클래스를 정의하는 경우 다음 섹션에서 설명한 대로 메서드를 구현 IDisposable.Dispose 하고 두 번째 오버로드를 Dispose
제공해야 합니다.
IDisposable 및 상속 계층 구조
삭제 가능해야 하는 하위 클래스가 있는 기본 클래스는 다음과 같이 구현 IDisposable 해야 합니다. (Visual Basic의 경우)NotInheritable
이 아닌 sealed
모든 형식에서 구현 IDisposable 할 때마다 이 패턴을 사용해야 합니다.
하나의 공용, 비 가상 Dispose() 메서드 및 보호된 가상
Dispose(Boolean disposing)
메서드를 제공해야 합니다.메서드는 Dispose() 를 호출
Dispose(true)
해야 하며 성능에 대한 종료를 표시하지 않아야 합니다.기본 형식은 종료자를 포함하지 않아야 합니다.
다음 코드 조각은 기본 클래스에 대한 삭제 패턴을 반영합니다. 형식이 메서드를 재정의하지 않는다고 가정합니다 Object.Finalize .
using System;
using System.IO;
using System.Runtime.InteropServices;
class BaseClass1 : IDisposable
{
// Flag: Has Dispose already been called?
bool disposed = false;
// Instantiate a FileStream instance.
FileStream fs = new FileStream("test.txt", FileMode.OpenOrCreate);
// Public implementation of Dispose pattern callable by consumers.
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
// Protected implementation of Dispose pattern.
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
fs.Dispose();
// Free any other managed objects here.
//
}
disposed = true;
}
}
open System
open System.IO
type BaseClass1() =
// Flag: Has Dispose already been called?
let mutable disposed = false
// Instantiate a FileStream instance.
let fs = new FileStream("test.txt", FileMode.OpenOrCreate)
interface IDisposable with
// Public implementation of Dispose pattern callable by consumers.
member this.Dispose() =
this.Dispose true
GC.SuppressFinalize this
// Implementation of Dispose pattern.
abstract Dispose: bool -> unit
override _.Dispose(disposing) =
if not disposed then
if disposing then
fs.Dispose()
// Free any other managed objects here.
disposed <- true
Imports System.IO
Imports System.Runtime.InteropServices
Class BaseClass1 : Implements IDisposable
' Flag: Has Dispose already been called?
Dim disposed As Boolean = False
' Instantiate a FileStream instance.
Dim fs As FileStream = New FileStream("test.txt", FileMode.OpenOrCreate)
' Public implementation of Dispose pattern callable by consumers.
Public Sub Dispose() _
Implements IDisposable.Dispose
Dispose(disposing:=True)
GC.SuppressFinalize(Me)
End Sub
' Protected implementation of Dispose pattern.
Protected Overridable Sub Dispose(disposing As Boolean)
If disposed Then Return
If disposing Then
fs.Dispose()
' Free any other managed objects here.
'
End If
disposed = True
End Sub
End Class
메서드를 재정의 Object.Finalize 하는 경우 클래스는 다음 패턴을 구현해야 합니다.
using System;
class BaseClass2 : IDisposable
{
// Flag: Has Dispose already been called?
bool disposed = false;
// Public implementation of Dispose pattern callable by consumers.
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
// Protected implementation of Dispose pattern.
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
// Free any other managed objects here.
//
}
// Free any unmanaged objects here.
//
disposed = true;
}
~BaseClass2()
{
Dispose(disposing: false);
}
}
open System
type BaseClass2() =
// Flag: Has Dispose already been called?
let mutable disposed = false
interface IDisposable with
// Public implementation of Dispose pattern callable by consumers.
member this.Dispose() =
this.Dispose true
GC.SuppressFinalize this
// Implementation of Dispose pattern.
abstract Dispose: bool -> unit
override _.Dispose(disposing) =
if not disposed then
if disposing then
// Free any other managed objects here.
()
// Free any unmanaged objects here.
disposed <- true
override this.Finalize() =
this.Dispose false
Class BaseClass : Implements IDisposable
' Flag: Has Dispose already been called?
Dim disposed As Boolean = False
' Public implementation of Dispose pattern callable by consumers.
Public Sub Dispose() _
Implements IDisposable.Dispose
Dispose(disposing:=True)
GC.SuppressFinalize(Me)
End Sub
' Protected implementation of Dispose pattern.
Protected Overridable Sub Dispose(disposing As Boolean)
If disposed Then Return
If disposing Then
' Free any other managed objects here.
'
End If
' Free any unmanaged objects here.
'
disposed = True
End Sub
Protected Overrides Sub Finalize()
Dispose(disposing:=False)
End Sub
End Class
서브 클래스는 다음과 같이 삭제 가능한 패턴을 구현해야 합니다.
Dispose(Boolean)
을 재정의하고 기본 클래스Dispose(Boolean)
구현을override 호출해야 합니다.필요한 경우 종료자를 제공할 수 있습니다. 종료자가
Dispose(false)
을 호출해야 합니다.
파생 클래스 자체는 인터페이스를 구현 IDisposable 하지 않으며 매개 변수 없는 Dispose 메서드를 포함하지 않습니다. 기본 클래스 Dispose(Boolean)
메서드만 재정의합니다.
다음 코드 조각은 파생 클래스에 대한 삭제 패턴을 반영합니다. 형식이 메서드를 재정의하지 않는다고 가정합니다 Object.Finalize .
using System;
using System.IO;
using System.Runtime.InteropServices;
class MyDerivedClass : MyBaseClass
{
// Flag: Has Dispose already been called?
bool disposed = false;
// Instantiate a FileStream instance.
FileStream fs = new FileStream("test.txt", FileMode.OpenOrCreate);
// Protected implementation of Dispose pattern.
protected override void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
fs.Dispose();
// Free any other managed objects here.
//
}
// Free any unmanaged objects here.
//
disposed = true;
// Call base class implementation.
base.Dispose(disposing);
}
}
open Microsoft.Win32.SafeHandles
open System
type MyDerivedClass() =
inherit MyBaseClass()
// Flag: Has Dispose already been called?
let mutable disposed = false
// Instantiate a FileStream instance.
let fs = new FileStream("test.txt", FileMode.OpenOrCreate)
// Implementation of Dispose pattern.
override _.Dispose(disposing) =
if not disposed then
if disposing then
fs.Dispose()
// Free any other managed objects here.
// Free any unmanaged objects here.
disposed <- true
// Call base class implementation.
base.Dispose disposing
Imports System.IO
Imports System.Runtime.InteropServices
Class DerivedClass2 : Inherits BaseClass2
' Flag: Has Dispose already been called?
Dim disposed As Boolean = False
' Instantiate a FileStream instance.
Dim fs As FileStream = New FileStream("test.txt", FileMode.OpenOrCreate)
' Protected implementation of Dispose pattern.
Protected Overrides Sub Dispose(disposing As Boolean)
If disposed Then Return
If disposing Then
fs.Dispose()
' Free any other managed objects here.
'
End If
' Free any unmanaged objects here.
'
disposed = True
' Call base class implementation.
MyBase.Dispose(disposing)
End Sub
End Class
메서드
Dispose() |
관리되지 않는 리소스의 확보, 해제 또는 다시 설정과 관련된 애플리케이션 정의 작업을 수행합니다. |