System.Net.NetworkInformation.Ping的实现可能是错误的。 [英] The implementation of System.Net.NetworkInformation.Ping may be wrong.

查看:66
本文介绍了System.Net.NetworkInformation.Ping的实现可能是错误的。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,正如预期的那样Ping类通过反射器查看非托管资源。在大多数情况下,正如我所知,该类实现了一个显式的IDisposable.Dispose方法。 Ping和其他人一样,就像EventWaitHandle一样。但是与EventWaitHandle不同,它还继承了Component,一个实现类,它实现了IDisposable的隐式Dispose。作为一个局外人,我很惊讶这样做:
Ping p = new Ping();
// ...
p.Dispose();
作为组件class遵循标准IDisposable模式。其中有一个受保护的虚拟Dispose(bool)方法。一个有趣的观察是Ping没有覆盖那个受保护的方法并且显示明确的IDisposable.Dispose如下:
//来自反射器(.net 2.0)
void IDisposable.Dispose()
{
InternalDispose();
}

InternalDispose()只关闭所有内容并返回。

所以如果有人像我一样,请打电话给Ping。 Dispose()会得到意想不到的结果。是真的吗?

解决方案

您的观察是正确的。这已在本周发布的.Net 4.0 Beta 2中得到修复。看一看,让我们知道您的想法。

http:// msdn .microsoft.com / EN-US / vstudio / dd582936.aspx


Hi there,
Ping class as expected holds an unmanaged resource viewed via reflector. In most cases, as i know, the class implements an explicit IDisposable.Dispose method. And Ping does it the same way as others, like EventWaitHandle. But unlike EventWaitHandle, it also inherits Component, an implementation class, which implement an implicit Dispose from IDisposable. As an outsider, i am surprising to do like this:
Ping p = new Ping();
//...
p.Dispose();
As the Component class follow standard IDisposable pattern. There is a protected virtual Dispose(bool) method inside it. An interesting observation is the Ping doesn't override that protected method and implment the explicit IDisposable.Dispose as follow:
//from reflector (.net 2.0)
void IDisposable.Dispose()
{
  InternalDispose();
}

The InternalDispose() just closes everything and returns.

So if anyone acts like me, call Ping.Dispose() will get unexpected result. Is it true?

解决方案

Your observations are correct.  This has been fixed in .Net 4.0 Beta 2, which was released this week.  Take a look and let us know what you think.

http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx


这篇关于System.Net.NetworkInformation.Ping的实现可能是错误的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆