c#扩展方法的使用,实现的几个功能

用扩展类写了一个管理类:

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop; namespace CommonClass
{
public static class ExtentionMethods
{
/// <summary>
/// dataRow 获取字段
/// </summary>
/// <param name="dr"></param>
/// <param name="fieldName"></param>
/// <returns></returns>
public static string GetField(this DataRow dr, string fieldName)
{
if (dr.Table.Columns.Contains(fieldName) && dr[fieldName] != null)
{
return dr[fieldName].ToString();
}
else
{
return "";
}
}
/// <summary>
/// Jtoken类型获取字符型字段
/// </summary>
/// <param name="token"></param>
/// <param name="fieldName"></param>
/// <returns></returns>
public static string GetStringField(this JToken token, string fieldName)
{
if (token[fieldName] == null)
{
return "";
}
else
{
return token[fieldName].ToString();
}
}
/// <summary>
/// dictionary添加键和值
/// </summary>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <param name="dict"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void AddKeyValue<T1, T2>(this Dictionary<T1, T2> dict, T1 key, T2 value)
{
if (!dict.ContainsKey(key))
{
dict.Add(key, value);
}
}
/// <summary>
/// 窗体显示在usercontrol上面
/// </summary>
/// <param name="wnd"></param>
/// <param name="uc"></param>
public static void ShowInUserControl(this Window wnd, UserControl uc)
{
HwndSource winformWindow =
(System.Windows.Interop.HwndSource.FromDependencyObject(uc) as System.Windows.Interop.HwndSource);
if (winformWindow != null)
new WindowInteropHelper(wnd) { Owner = winformWindow.Handle };
wnd.Show();
}
/// <summary>
/// 将byte[] 反序列化成object对象
/// </summary>
/// <param name="binaryData">字节数组</param>
/// <returns>object对象</returns>
public static object ToEntity(this byte[] binaryData)
{
MemoryStream ms = new MemoryStream(binaryData);
ms.Seek(0, SeekOrigin.Begin);
IFormatter brFormatter = new BinaryFormatter(); Object obj = brFormatter.Deserialize(ms);
return obj;
}
/// <summary>
/// 将object 序列化成字节数组byte[]
/// </summary>
/// <param name="dsOriginal">object对象</param>
/// <returns>字节数组</returns>
public static byte[] ToByteArray(this object dsOriginal)
{
byte[] binaryDataResult = null;
MemoryStream memStream = new MemoryStream();
IFormatter brFormatter = new BinaryFormatter();
brFormatter.Serialize(memStream, dsOriginal);
binaryDataResult = memStream.ToArray();
memStream.Close();
memStream.Dispose();
return binaryDataResult;
} /// <summary>
/// 克隆-序列化的类
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static T Clone<T>(this T obj)
{
BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, obj); ms.Seek(0, SeekOrigin.Begin); return (T)bf.Deserialize(ms);
} /// <summary>
/// 获取-窗口指针
/// </summary>
/// <param name="win"></param>
/// <returns></returns>
public static IntPtr ToHandle(this Window win)
{
WindowInteropHelper wndHelper = new WindowInteropHelper(win);
IntPtr wpfHwnd = wndHelper.Handle;
return wpfHwnd;
}
/// <summary>
/// 文本ip(0.0.0.0格式) 获取数字ip
/// </summary>
/// <param name="ipString"></param>
/// <returns></returns>
public static UInt32 ToIntIp(this string ipString)
{
UInt32 ip = (UInt32)(IPAddress.Parse(ipString).Address);
return ip;
} }
}

感谢每一位阅读此篇文章的人,希望可以帮到你。

上一篇:转载 LayoutInflater的inflate函数用法详解


下一篇:Android UiAutomator快速调试