using System.Net; using System.Windows.Forms; namespace NetWorkHelper.ICommond { public class CommonMethod { /// /// 域名转换为IP地址 /// /// 域名或IP地址 /// IP地址 internal static string HostnameToIp(string hostname) { try { IPAddress ip; if (IPAddress.TryParse(hostname, out ip)) return ip.ToString(); else return Dns.GetHostEntry(hostname).AddressList[0].ToString(); } catch { throw; } } /// /// 外部调用是否需要用Invoket /// /// 事件参数 internal static void EventInvoket(Action func) { Form form = null; if (Application.OpenForms.Count > 0) { form = Application.OpenForms[0]; } //Form form = Application.OpenForms.Cast
().FirstOrDefault(); if (form != null && form.InvokeRequired) { form.Invoke(func); } else { func(); } } /// /// 具有返回值的 非bool 外部调用是否需要用Invoket /// /// 方法 /// 返回客户操作之后的数据 internal static object EventInvoket(Func func) { object haveStr; Form form = null; if (Application.OpenForms.Count > 0) { form = Application.OpenForms[0]; } //Form form = Application.OpenForms.Cast().FirstOrDefault(); if (form != null && form.InvokeRequired) { haveStr = form.Invoke(func); } else { haveStr = func(); } return haveStr; } public delegate void Action(); public delegate TResult Func(); public delegate TResult Func(T a); public delegate TResult Func(T1 arg1, T2 arg2); public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3); public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4); } }