idisposable

Disposing the members that implement IDisposable

我怕爱的太早我们不能终老 提交于 2020-01-02 01:47:08
问题 In my Dispose methods (like the one below), everytime i want to call someObj.Dispose() i also have a check for someObj!=null. Is that because of bad design on my part? Is their a cleaner way to ascertain that Dispose of all the members (implementing IDisposable) being used in an object is called without having a risk of NullReference exception ? protected void Dispose(bool disposing) { if (disposing) { if (_splitTradePopupManager != null) { _splitTradePopupManager.Dispose(); } } } Thanks for

IEnumerator: Is it normal to have an empty Dispose method?

我的未来我决定 提交于 2020-01-02 01:08:06
问题 I'm writing an IEnumerator<T> class to iterate over a COM collection I'm wrappering. I've noticed that IEnumerator<T> extends IDisposable, so I'm required to implement the Dispose method. However, I can't think of anything I would put there, as I only have a reference to the collection (which I wouldn't want being disposed at the end of a foreach ), and an int for the index. Is it normal to leave the Dispose method empty? 回答1: Yes, it is. IEnumerator<T> implements IDisposable in case you make

How do I correctly manage the disposing of a DataContext?

删除回忆录丶 提交于 2020-01-01 05:12:45
问题 I have a web service that is quite heavy on database access. It works fine in test, but as soon as I put it in production and ramp up the load it starts churning out errors that are raised when something calls a method in the DataContext. The error is normally one of these: Object reference not set to an instance of an object Cannot access a disposed object. Object name: 'DataContext accessed after Dispose.'. but not always. Any single web service requests can result as many as 10 or 15

Generic function to handle disposing IDisposable objects

白昼怎懂夜的黑 提交于 2020-01-01 04:42:11
问题 I am working on a class that deals with a lot of Sql objects - Connection, Command, DataAdapter, CommandBuilder, etc. There are multiple instances where we have code like this: if( command != null ) { command.Dispose(); } if( dataAdapter != null ) { dataAdapter.Dispose(); } etc I know this is fairly insufficient in terms of duplication, but it has started smelling. The reason why I think it smells is because in some instances the object is also set to null. if( command != null ) { command

Avoiding disposing system-defined Pen and Brush instances

大憨熊 提交于 2020-01-01 04:39:06
问题 I understand it is best practise to call Dispose() on instances of Pen and Brush, except if they've been set to the system-predefined values (eg. System.Drawing.Brushes, System.Drawing.Pens or System.Drawing.SystemBrushes) Trying to dispose a system-defined resource results in an exception being thrown. It doesn't appear to be obvious how you can detect (apart from wrapping the Dispose() call in a try/catch) whether one of these resources is referencing a system-defined or user-defined value.

Autofac: How to limit the lifetime of an IDisposable object without passing around the IoC container

本秂侑毒 提交于 2020-01-01 02:39:31
问题 I'm currently learning how to use Autofac, and I'm stuck with disposing IDisposable objects deterministically. Let me first present the situation before I'll state my problem. Starting position: Let's say my object model is defined through the following interfaces: interface IApple : IDisposable { void Consume(); } interface IHorse { void Eat(IApple apple); // is supposed to call apple.Consume() } interface IHorseKeeper { void FeedHorse(); // is supposed to call horse.Eat(apple) // where

IDisposable on an injected repository

大城市里の小女人 提交于 2019-12-31 16:40:17
问题 I have the following ADO .Net Repository public class Repository : IRepository, IDisposable { private readonly IUnitOfWork UnitOfWork; private SqlConnection Connection; public Repository(IUnitOfWork unitOfWork, connectionString) { UnitOfWork = unitOfWork; Connection = new SqlConnection(connectionString); Connection.Open(); } public MyObject FindBy(string userName) { //...Ado .Net command.ExecuteReader, etc. } } This repository is injected with an IoC container to a Domain Service and is used

How do you dispose of an IDisposable in Managed C++

我的梦境 提交于 2019-12-30 07:54:07
问题 I'm trying to Dispose of an IDisposable object(FileStream^ fs) in managed C++ (.Net 2.0) and am getting the error 'Dispose' : is not a member of 'System::IO::FileStream' It says that I should invoke the destructor instead. Will calling fs->~FileStream(); call the dispose method on the FileStream object? Why can't I call Dispose? 回答1: The correct pattern is to just delete the object: delete fs; This will be translated into a call to Dispose() See this post for some of the details of what is

Is this IDisposable implementation correct?

拈花ヽ惹草 提交于 2019-12-30 07:33:27
问题 I can never remember all the rules for implementing the IDisposable interface, so I tried to come up with a base class that takes care of all of this and makes IDisposable easy to implement. I just wanted to hear your opinion if this implementation is ok as it is or whether you see something I could improve. The user of this base class is supposed to derive from it and then implement the two abstract methods ReleaseManagedResources() and ReleaseUnmanagedResources() . So here is the code:

How can I dispose System.Xml.XmlWriter in PowerShell

隐身守侯 提交于 2019-12-30 04:27:11
问题 I am trying to dispose XmlWriter object: try { [System.Xml.XmlWriter] $writer = [System.Xml.XmlWriter]::Create('c:\some.xml') } finally { $writer.Dispose() } Error: Method invocation failed because [System.Xml.XmlWellFormedWriter] doesn't contain a method named 'Dispose'. On the other side: $writer -is [IDisposable] # True What should I do? 回答1: Dispose is protected on System.Xml.XmlWriter . You should use Close instead. $writer.Close 回答2: Here is an alternative approach: (get-interface $obj