开发者

Which .NET Framework classes implement IDisposable

开发者 https://www.devze.com 2023-03-06 08:46 出处:网络
Seems like this has to be documented somewhere but I\'m not finding is anywhere.Perhaps my Google-fu is 开发者_如何学Goweakening.You realize that this will depend on what you call .NET Framework class

Seems like this has to be documented somewhere but I'm not finding is anywhere. Perhaps my Google-fu is 开发者_如何学Goweakening.


You realize that this will depend on what you call .NET Framework classes. You probably might want to specify which assemblies you are looking for. Armed with this information you can load those assemblies and use reflection to list all public types that implement IDisposable in a given assembly. Let's take the System assembly as example:

class Program
{
    static void Main()
    {
        var types = Assembly
            .Load("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
            .GetTypes()
            .Where(t => typeof(IDisposable).IsAssignableFrom(t))
            .OrderBy(t => t.Name);
        foreach (var type in types)
        {
            Console.WriteLine(type);
        }
    }
}


Using the code proposed by Darin Dimitrov, here is the list of framework Classes that implement IDisposable in a simple C# console application:

Microsoft.CSharp.CSharpCodeProvider Microsoft.VisualBasic.VBCodeProvider Microsoft.Win32.SafeHandles.SafeEventLogReadHandle Microsoft.Win32.SafeHandles.SafeEventLogWriteHandle Microsoft.Win32.SafeHandles.SafeFileMappingHandle Microsoft.Win32.SafeHandles.SafeFileMapViewHandle Microsoft.Win32.SafeHandles.SafeLibraryHandle Microsoft.Win32.SafeHandles.SafeLocalMemHandle Microsoft.Win32.SafeHandles.SafeProcessHandle Microsoft.Win32.SafeHandles.SafeThreadHandle Microsoft.Win32.SafeHandles.SafeTimerHandle Microsoft.Win32.SafeHandles.SafeUserTokenHandle Microsoft.Win32.WinInetCache+ReadStream Microsoft.Win32.WinInetCache+WriteStream System.CodeDom.Compiler.CodeDomProvider System.CodeDom.Compiler.IndentedTextWriter System.CodeDom.Compiler.TempFileCollection System.Collections.Concurrent.BlockingCollection1 System.Collections.Generic.LinkedList1+Enumerator System.Collections.Generic.Queue1+Enumerator System.Collections.Generic.SortedDictionary2+Enumerator System.Collections.Generic.SortedDictionary2+KeyCollection+Enumerator System.Collections.Generic.SortedDictionary2+ValueCollection+Enumerator System.Collections.Generic.SortedList2+Enumerator System.Collections.Generic.SortedList2+SortedListKeyEnumerator System.Collections.Generic.SortedList2+SortedListValueEnumerator System.Collections.Generic.SortedSet1+d__12 System.Collections.Generic.SortedSet1+Enumerator System.Collections.Generic.Stack1+Enumerator System.Collections.ObjectModel.ObservableCollection1+SimpleMonitor System.ComponentModel.BackgroundWorker System.ComponentModel.Component System.ComponentModel.Container System.ComponentModel.Design.DesignerTransaction System.ComponentModel.Design.IDesigner System.ComponentModel.Design.IRootDesigner System.ComponentModel.Design.ITreeDesigner System.ComponentModel.Design.Serialization.SerializationStore System.ComponentModel.Design.ServiceContainer System.ComponentModel.EventHandlerList System.ComponentModel.IComponent System.ComponentModel.IContainer System.ComponentModel.INestedContainer System.ComponentModel.License System.ComponentModel.LicFileLicenseProvider+LicFileLicense System.ComponentModel.MarshalByValueComponent System.ComponentModel.NestedContainer System.Configuration.ClientSettingsStore+QuotaEnforcedStream System.Diagnostics.AsyncStreamReader System.Diagnostics.ConsoleTraceListener System.Diagnostics.DefaultTraceListener System.Diagnostics.DelimitedListTraceListener System.Diagnostics.EventLog System.Diagnostics.EventLogEntry System.Diagnostics.EventLogInternal System.Diagnostics.EventLogTraceListener System.Diagnostics.PerformanceCounter System.Diagnostics.Process System.Diagnostics.ProcessModule System.Diagnostics.ProcessThread System.Diagnostics.ProcessWaitHandle System.Diagnostics.TextWriterTraceListener System.Diagnostics.TraceListener System.Diagnostics.XmlWriterTraceListener System.IO.Compression.DeflateStream System.IO.Compression.GZipStream System.IO.FileSystemWatcher System.IO.Ports.SerialPort System.IO.Ports.SerialStream System.Media.SoundPlayer System.Net.AsyncRequestContext System.Net.Base64Stream System.Net.BaseWebProxyFinder System.Net.BufferedReadStream System.Net.Cache.BaseWrapperStream System.Net.Cache.CombinedReadStream System.Net.Cache.ForwardingReadStream System.Net.Cache.MetadataUpdateStream System.Net.Cache.RangeStream System.Net.Cache.SingleItemRequestCache+ReadOnlyStream System.Net.Cache.SingleItemRequestCache+WriteOnlyStream System.Net.ClosableStream System.Net.CommandStream System.Net.Connection System.Net.ConnectStream System.Net.DeflateWrapperStream System.Net.DelegatedStream System.Net.DirectProxy System.Net.FileWebResponse System.Net.FileWebStream System.Net.FtpControlStream System.Net.FtpDataStream System.Net.FtpWebResponse System.Net.FtpWebResponse+EmptyStream System.Net.GZipWrapperStream System.Net.HttpListener System.Net.HttpListenerResponse System.Net.HttpRequestQueueV2Handle System.Net.HttpRequestStream System.Net.HttpResponseStream System.Net.HttpServerSessionHandle System.Net.HttpWebResponse System.Net.HybridWebProxyFinder System.Net.IWebProxyFinder System.Net.ListenerPrefixEnumerator System.Net.Mail.AlternateView System.Net.Mail.AlternateViewCollection System.Net.Mail.Attachment System.Net.Mail.AttachmentBase System.Net.Mail.AttachmentCollection System.Net.Mail.LinkedResource System.Net.Mail.LinkedResourceCollection System.Net.Mail.MailMessage System.Net.Mail.SmtpClient System.Net.Mail.SmtpPooledStream System.Net.Mime.MimePart System.Net.Mime.QEncodedStream System.Net.Mime.QuotedPrintableStream System.Net.Mime.SevenBitStream System.Net.NetWebProxyFinder System.Net.NetworkAddressChangePolled System.Net.NetworkInformation.Ping System.Net.NetworkInformation.SafeCancelMibChangeNotify System.Net.NetworkInformation.SafeFreeMibTable System.Net.PooledStream System.Net.ProxyChain System.Net.ProxyChain+ProxyEnumerator System.Net.ProxyScriptChain System.Net.RequestContextBase System.Net.SafeCloseHandle System.Net.SafeCloseIcmpHandle System.Net.SafeCloseSocket System.Net.SafeCloseSocket+InnerSafeCloseSocket System.Net.SafeCloseSocketAndEvent System.Net.SafeCredentialReference System.Net.SafeDeleteContext System.Net.SafeDeleteContext_SCHANNEL System.Net.SafeDeleteContext_SECUR32 System.Net.SafeDeleteContext_SECURITY System.Net.SafeFreeAddrInfo System.Net.SafeFreeCertChain System.Net.SafeFreeCertContext System.Net.SafeFreeContextBuffer System.Net.SafeFreeContextBuffer_SCHANNEL System.Net.SafeFreeContextBuffer_SECUR32 System.Net.SafeFreeContextBuffer_SECURITY System.Net.SafeFreeContextBufferChannelBinding System.Net.SafeFreeContextBufferChannelBinding_SCHANNEL System.Net.SafeFreeContextBufferChannelBinding_SECUR32 System.Net.SafeFreeContextBufferChannelBinding_SECURITY System.Net.SafeFreeCredential_SCHANNEL System.Net.SafeFreeCredential_SECUR32 System.Net.SafeFreeCredential_SECURITY System.Net.SafeFreeCredentials System.Net.SafeGlobalFree System.Net.SafeInternetHandle System.Net.SafeLoadLibrary System.Net.SafeLocalFree System.Net.SafeLocalFreeChannelBinding System.Net.SafeNativeOverlapped System.Net.SafeOverlappedFree System.Net.SafeRegistryHandle System.Net.SafeUnlockUrlCacheEntryFile System.Net.Security.AuthenticatedStream System.Net.Security.NegotiateStream System.Net.Security.SslStream System.Net.Semaphore System.Net.Sockets.NetworkStream System.Net.Sockets.Socket System.Net.Sockets.SocketAsyncEventArgs System.Net.Sockets.TcpClient System.Net.Sockets.UdpClient System.Net.StaticProxy System.Net.SyncMemoryStream System.Net.SyncRequestContext System.Net.TimerThread+InfiniteTimer System.Net.TimerThread+Timer System.Net.TimerThread+TimerNode System.Net.TlsStream System.Net.WebClient System.Net.WebClient+WebClientWriteStream System.Net.WebResponse System.Net.WinHttpWebProxyFinder System.Security.Authentication.ExtendedProtection.ChannelBinding System.Security.Cryptography.SafeCertChainHandle System.Security.Cryptography.SafeCertContextHandle System.Security.Cryptography.SafeCertStoreHandle System.Security.Cryptography.SafeCryptMsgHandle System.Security.Cryptography.SafeCryptProvHandle System.Security.Cryptography.SafeLibraryHandle System.Security.Cryptography.SafeLocalAllocHandle System.Threading.Barrier System.Threading.Semaphore System.Timers.Timer

And this is the list for System.Windows.Forms:

System.Deployment.Internal.Isolation.BLOB System.Deployment.Internal.Isolation.Manifest.AssemblyReferenceDependentAssemblyEntry System.Deployment.Internal.Isolation.Manifest.DirectoryEntry System.Deployment.Internal.Isolation.Manifest.FileEntry System.Deployment.Internal.Isolation.Manifest.HashElementEntry System.Deployment.Internal.Isolation.Manifest.MetadataSectionEntry System.Deployment.Internal.Isolation.Manifest.MuiResourceMapEntry System.Deployment.Internal.Isolation.Manifest.MuiResourceTypeIdIntEntry System.Deployment.Internal.Isolation.Manifest.MuiResourceTypeIdStringEntry System.Deployment.Internal.Isolation.Manifest.RegistryKeyEntry System.Deployment.Internal.Isolation.Store+ApplicationPathLock System.Deployment.Internal.Isolation.Store+AssemblyPathLock System.Deployment.Internal.Isolation.Store+IPathLock System.Deployment.Internal.Isolation.StoreTransaction System.Resources.ResXResourceReader System.Resources.ResXResourceSet System.Resources.ResXResourceWriter System.Security.Policy.TrustManagerMoreInformation System.Security.Policy.TrustManagerPromptUI System.Windows.Forms.Application+MarshalingControl System.Windows.Forms.Application+ModalApplicationContext System.Windows.Forms.Application+ParkingWindow System.Windows.Forms.ApplicationContext System.Windows.Forms.ArrangedElement System.Windows.Forms.AxHost System.Windows.Forms.AxHost+OleInterfaces System.Windows.Forms.BindingNavigator System.Windows.Forms.BindingSource System.Windows.Forms.Button System.Windows.Forms.ButtonBase System.Windows.Forms.CachedItemHdcInfo System.Windows.Forms.CheckBox System.Windows.Forms.CheckedListBox System.Windows.Forms.ColorDialog System.Windows.Forms.ColumnHeader System.Windows.Forms.ComboBox System.Windows.Forms.CommonDialog System.Windows.Forms.ContainerControl System.Windows.Forms.ContextMenu System.Windows.Forms.ContextMenuStrip System.Windows.Forms.Control System.Windows.Forms.Control+ActiveXImpl+AdviseHelper+ComConnectionPoint System.Windows.Forms.Control+ActiveXImpl+AdviseHelper+ComConnectionPointContainer System.Windows.Forms.Control+ActiveXImpl+AdviseHelper+SafeIUnknown System.Windows.Forms.Control+FontHandleWrapper System.Windows.Forms.Control+MetafileDCWrapper System.Windows.Forms.Control+MultithreadSafeCallScope System.Windows.Forms.Control+PrintPaintEventArgs System.Windows.Forms.Cursor System.Windows.Forms.DataGrid System.Windows.Forms.DataGridBoolColumn System.Windows.Forms.DataGridColumnStyle System.Windows.Forms.DataGridTableStyle System.Windows.Forms.DataGridTextBox System.Windows.Forms.DataGridTextBoxColumn System.Windows.Forms.DataGridView System.Windows.Forms.DataGridViewBand System.Windows.Forms.DataGridViewButtonCell System.Windows.Forms.DataGridViewButtonColumn System.Windows.Forms.DataGridViewCell System.Windows.Forms.DataGridViewCheckBoxCell System.Windows.Forms.DataGridViewCheckBoxColumn System.Windows.Forms.DataGridViewColumn System.Windows.Forms.DataGridViewColumnHeaderCell System.Windows.Forms.DataGridViewComboBoxCell System.Windows.Forms.DataGridViewComboBoxColumn System.Windows.Forms.DataGridViewComboBoxEditingControl System.Windows.Forms.DataGridViewHeaderCell System.Windows.Forms.DataGridViewImageCell System.Windows.Forms.DataGridViewImageColumn System.Windows.Forms.DataGridViewLinkCell System.Windows.Forms.DataGridViewLinkColumn System.Windows.Forms.DataGridViewRow System.Windows.Forms.DataGridViewRowHeaderCell System.Windows.Forms.DataGridViewTextBoxCell System.Windows.Forms.DataGridViewTextBoxColumn System.Windows.Forms.DataGridViewTextBoxEditingControl System.Windows.Forms.DataGridViewTopLeftHeaderCell System.Windows.Forms.DataStreamFromComStream System.Windows.Forms.DateTimePicker System.Windows.Forms.Design.ComponentEditorForm System.Windows.Forms.Design.ComponentEditorForm+PageSelector System.Windows.Forms.Design.ComponentEditorPage System.Windows.Forms.DomainUpDown System.Windows.Forms.ErrorProvider System.Windows.Forms.FileDialog System.Windows.Forms.FlowLayoutPanel System.Windows.Forms.FolderBrowserDialog System.Windows.Forms.FontDialog System.Windows.Forms.Form System.Windows.Forms.Form+SecurityToolTip System.Windows.Forms.GroupBox System.Windows.Forms.HelpProvider System.Windows.Forms.HScrollBar System.Windows.Forms.HtmlDocument+HtmlDocumentShim System.Windows.Forms.HtmlElement+HtmlElementShim System.Windows.Forms.HtmlHistory System.Windows.Forms.HtmlShim System.Windows.Forms.HtmlShimManager System.Windows.Forms.HtmlWindow+HtmlWindowShim System.Windows.Forms.IBindableComponent System.Windows.Forms.ImageList System.Windows.Forms.ImageList+NativeImageList System.Windows.Forms.ImageListStreamer System.Windows.Forms.Internal.DeviceContext System.Windows.Forms.Internal.WindowsBrush System.Windows.Forms.Internal.WindowsFont System.Windows.Forms.Internal.WindowsGraphics System.Windows.Forms.Internal.WindowsPen System.Windows.Forms.Internal.WindowsRegion System.Windows.Forms.Internal.WindowsSolidBrush System.Windows.Forms.Label System.Windows.Forms.Layout.IArrangedElement System.Windows.Forms.Layout.LayoutTransaction System.Windows.Forms.Layout.NullLayoutTransaction System.Windows.Forms.LinkLabel System.Windows.Forms.ListBox System.Windows.Forms.ListControl System.Windows.Forms.ListView System.Windows.Forms.MainMenu System.Windows.Forms.MaskedTextBox System.Windows.Forms.MdiClient System.Windows.Forms.MdiControlStrip System.Windows.Forms.MdiControlStrip+ControlBoxMenuItem System.Windows.Forms.MdiControlStrip+SystemMenuItem System.Windows.Forms.MdiWindowDialog System.Windows.Forms.MdiWindowListStrip System.Windows.Forms.Menu System.Windows.Forms.MenuItem System.Windows.Forms.MenuStrip System.Windows.Forms.MonthCalendar System.Windows.Forms.MouseHoverTimer System.Windows.Forms.NotifyIcon System.Windows.Forms.NumericUpDown System.Windows.Forms.OpenFileDialog System.Windows.Forms.PageSetupDialog System.Windows.Forms.PaintEventArgs System.Windows.Forms.Panel System.Windows.Forms.PictureBox System.Windows.Forms.PrintControllerWithStatusDialog+StatusDialog System.Windows.Forms.PrintDialog System.Windows.Forms.PrintPreviewControl System.Windows.Forms.PrintPreviewDialog System.Windows.Forms.ProgressBar System.Windows.Forms.PropertyGrid System.Windows.Forms.PropertyGrid+SnappableControl System.Windows.Forms.PropertyGridInternal.DocComment System.Windows.Forms.PropertyGridInternal.DropDownButton System.Windows.Forms.PropertyGridInternal.GridErrorDlg System.Windows.Forms.PropertyGridInternal.GridToolTip System.Windows.Forms.PropertyGridInternal.HotCommands System.Windows.Forms.PropertyGridInternal.PropertyGridView System.Windows.Forms.PropertyGridInternal.PropertyGridView+DropDownHolder System.Windows.Forms.PropertyGridInternal.PropertyGridView+GridViewEdit System.Windows.Forms.PropertyGridInternal.PropertyGridView+GridViewListBox System.Windows.Forms.RadioButton System.Windows.Forms.RichTextBox System.Windows.Forms.SaveFileDialog System.Windows.Forms.ScrollableControl System.Windows.Forms.ScrollBar System.Windows.Forms.SendKeys+SKWindow System.Windows.Forms.SplitContainer System.Windows.Forms.Splitter System.Windows.Forms.SplitterPanel System.Windows.Forms.StatusBar System.Windows.Forms.StatusBarPanel System.Windows.Forms.StatusStrip System.Windows.Forms.StatusStrip+RightToLeftLayoutGrip System.Windows.Forms.TabControl System.Windows.Forms.TableLayoutCellPaintEventArgs System.Windows.Forms.TableLayoutPanel System.Windows.Forms.TabPage System.Windows.Forms.TextBox System.Windows.Forms.TextBoxBase System.Windows.Forms.ThreadExceptionDialog System.Windows.Forms.Timer System.Windows.Forms.ToolBar System.Windows.Forms.ToolBarButton System.Windows.Forms.ToolStrip System.Windows.Forms.ToolStripButton System.Windows.Forms.ToolStripComboBox System.Windows.Forms.ToolStripComboBox+ToolStripComboBoxControl System.Windows.Forms.ToolStripContainer System.Windows.Forms.ToolStripContentPanel System.Windows.Forms.ToolStripControlHost System.Windows.Forms.ToolStripDropDown System.Windows.Forms.ToolStripDropDownButton System.Windows.Forms.ToolStripDropDownItem System.Windows.Forms.ToolStripDropDownMenu System.Windows.Forms.ToolStripGrip System.Windows.Forms.ToolStripItem System.Windows.Forms.ToolStripLabel System.Windows.Forms.ToolStripMenuItem System.Windows.Forms.ToolStripOverflow System.Windows.Forms.ToolStripOverflowButton System.Windows.Forms.ToolStripPanel System.Windows.Forms.ToolStripPanel+FeedbackRectangle System.Windows.Forms.ToolStripPanel+FeedbackRectangle+FeedbackDropDown System.Windows.Forms.ToolStripPanelCell System.Windows.Forms.ToolStripPanelRow System.Windows.Forms.ToolStripProgressBar System.Windows.Forms.ToolStripScrollButton System.Windows.Forms.ToolStripScrollButton+StickyLabel System.Windows.Forms.ToolStripSeparator System.Windows.Forms.ToolStripSplitButton System.Windows.Forms.ToolStripSplitButton+ToolStripSplitButtonButton System.Windows.Forms.ToolStripStatusLabel System.Windows.Forms.ToolStripTextBox System.Windows.Forms.ToolStripTextBox+ToolStripTextBoxControl System.Windows.Forms.ToolTip System.Windows.Forms.ToolTip+ToolTipTimer System.Windows.Forms.TrackBar System.Windows.Forms.TreeView System.Windows.Forms.UpDownBase System.Windows.Forms.UpDownBase+UpDownButtons System.Windows.Forms.UpDownBase+UpDownEdit System.Windows.Forms.UserControl System.Windows.Forms.VisualStyles.VisualStyleRenderer+ThemeHandle System.Windows.Forms.VScrollBar System.Windows.Forms.WebBrowser System.Windows.Forms.WebBrowser+WebBrowserSite System.Windows.Forms.WebBrowserBase System.Windows.Forms.WebBrowserSiteBase System.Windows.Forms.WindowsFormsSynchronizationContext System.Windows.Forms.WindowsFormsUtils+DCMapping System.Windows.Forms.WindowsGraphicsWrapper


Why would you want a list of all classes the implement IDisposable? It seems the more relevant question is "How can I tell if a class implements IDisposable?" If you need documentation, MSDN will tell you for a given class which interfaces it implements. If you need to know at compile-time, the compiler will yell at you if you try to wrap an object that isn't an instance of a class that implements IDisposable in a using block. If you need to know at runtime, you can use var disposable = obj as IDisposable and check if disposable is null or not.

If you have a specific assembly that you want to know about, you can rip through its types using reflection and test if those types implement IDisposable or not (use

typeof(IDisposable).IsAssignableFrom(type)

where type is an instance of Type that you've loaded from some Assembly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号