例子: // 定义Delegate类型,约束事件程序的参数
public delegate void MyEventHandler(object sender, long lineNumber) ;
public class DataImports
{
// 定义新事件NewLineRead
public event MyEventHandler NewLineRead ;
public void ImportData()
{
long i = 0 ; // 事件参数
while()
{
i ;
// 触发事件
if( NewLineRead != null ) NewLineRead(this, i);
//...
}
//...
}
//...
}
// 以下为Client代码
private void CallMethod()
{
// 声明Class变量,不需要WithEvents
private DataImports _da = null;
// 指定事件处理程序
_da.NewLineRead = new MyEventHandler(this.DA_EnterNewLine) ;
// 调用Class方法,途中会触发事件
_da.ImportData();
}
// 事件处理程序
private void DA_EnterNewLine(object sender, long lineNumber)
{
// ...
}
三、IP与主机名解析
使用System.Net可以实现与Ping命令行类似的IP解析功能,例如将主机名解析为IP或者反过来: private string GetHostNameByIP(string ipAddress)
{
IPHostEntry hostInfo = Dns.GetHostByAddress(ipAddress);
return hostInfo.HostName;
}
private string GetIPByHostName(string hostName)
{
System.Net.IPHostEntry hostInfo = Dns.GetHostByName(hostName);
return hostInfo.AddressList[0].ToString();
}
http://www.cnblogs.com/xh831213/archive/2006/08/31/491425.html
评论加载中…
![]() |