Why is the Dispose () method not available for my IDisposable?

I have a class with this field:

private WcfChannelFactory<IPrestoService> _channelFactory; 

In the Dispose() method, I do this:

 if (_channelFactory != null) { _channelFactory.Dispose(); } 

But this causes an error:

Unable to access explicit implementation of IDisposable.Dispose

After doing the research, it turns out that I can dispose of as follows:

 if (_channelFactory != null) { (_channelFactory as IDisposable).Dispose(); } 

I do not understand two things:

  • Why not Dispose() ? WcfChannelFactory<T> comes from ChannelFactory<T> , which comes from ChannelFactory , which implements IDisposable . However, ChannelFactory does not have a Dispose() method. How is this possible?

  • If I could (should?) Just call Close() on _channelFactory , why isn't the XML documentation saying Close() also calls Dispose() ? Maybe this is not so? This is confusing.

+6
source share
2 answers
  • Since the Dispose method is implemented for the IDisposable explicilty interface, you can only see this method when you have a link of type IDisposable . There is a method, but you cannot see it when you have a different type of link. This is similar to how a private method is accessible only from code inside the class itself, although it is always present.

  • The Close method will not call Dispose on this class. The method does not close the factory, it starts the asynchronous close process. When the Close method completes, the close process does not end, so the object cannot be deleted at this time.

+7
source

The Dispose method is implemented as an explicit member of the IDisposable interface. That is, the definition looks something like this:

 public class WcfChannelFactory<T> : IDisposable { public void IDisposable.Dispose() { ... } } 

Tutorial: Explicit Interface Tutorial

+2
source

Source: https://habr.com/ru/post/944493/


All Articles