物件深拷貝
1 /// <summary> 2 /// 深度克隆物件 3 /// </summary> 4 /// <typeparam name="T">待克隆型別(必須由[Serializable]修飾)</typeparam> 5 /// <param name="obj">待克隆物件</param> 6 /// <returns>新物件</returns> 7 public static T Clone<T>(T obj) 8 { 9 T ret = default(T); 10 if (obj != null) 11 { 12 XmlSerializer cloner = new XmlSerializer(typeof(T)); 13 MemoryStream stream = new MemoryStream(); 14 cloner.Serialize(stream, obj); 15 stream.Seek(0, SeekOrigin.Begin); 16 ret = (T)cloner.Deserialize(stream); 17 } 18 return ret; 19 } 20 /// <summary> 21 /// 克隆物件 22 /// </summary> 23 /// <param name="obj">待克隆物件(必須由[Serializable]修飾)</param> 24 /// <returns>新物件</returns> 25 public static object CloneObject(object obj) 26 { 27 using (MemoryStream memStream = new MemoryStream()) 28 { 29 BinaryFormatter binaryFormatter = new BinaryFormatter(null,new StreamingContext(StreamingContextStates.Clone)); 30 binaryFormatter.Serialize(memStream, obj); 31 memStream.Seek(0, SeekOrigin.Begin); 32 return binaryFormatter.Deserialize(memStream); 33 } 34 }
計算最大公約數
1 /// <summary> 2 /// 計算最大公約數 3 /// </summary> 4 /// <param name="a">數值A</param> 5 /// <param name="b">數值B</param> 6 /// <returns>最大公約數</returns> 7 private static ulong GCD(ulong a, ulong b) 8 { 9 return b == 0 ? a : GCD(b, a % b); 10 }
陣列與結構體相互轉換
1 /// <summary> 2 /// UDS報文結構體(示例) 3 /// </summary> 4 [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] 5 public struct StdUDSMsg 6 { 7 public byte DL; 8 public byte SID; 9 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] 10 public byte[] DID; 11 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] 12 public byte[] Data; 13 } 14 /// <summary> 15 /// byte 陣列轉結構體 16 /// </summary> 17 /// <typeparam name="T">結構體物件型別</typeparam> 18 /// <param name="bytes">byte 陣列</param> 19 /// <returns>結構體物件</returns> 20 public T ByteArrayToStructure<T>(byte[] bytes) where T : struct 21 { 22 T stuff = new T(); 23 GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned); 24 try 25 { 26 stuff = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T)); 27 } finally 28 { 29 handle.Free(); 30 } 31 return stuff; 32 } 33 /// <summary> 34 /// 結構體物件轉 Byte 陣列 35 /// </summary> 36 /// <typeparam name="T">結構體物件型別</typeparam> 37 /// <param name="stuff">結構體物件</param> 38 /// <returns>陣列</returns> 39 public byte[] StructureToByteArray<T>(T stuff) where T : struct 40 { 41 int size = 0; 42 IntPtr? msgPtr = null; 43 byte[] resBuf = null; 44 try 45 { 46 size = Marshal.SizeOf(stuff); 47 resBuf = new byte[size]; 48 msgPtr = Marshal.AllocHGlobal(size); 49 Marshal.StructureToPtr(stuff, msgPtr.Value, false); 50 Marshal.Copy(msgPtr.Value, resBuf, 0, size); 51 } finally 52 { 53 if (msgPtr != null) 54 { 55 Marshal.FreeHGlobal(msgPtr.Value); 56 } 57 } 58 return resBuf; 59 }
字串與char陣列、byte陣列轉換
1 /// <summary> 2 /// 將char陣列空餘部分填充預設值 3 /// </summary> 4 /// <param name="data">目標Char陣列</param> 5 /// <param name="defLength">陣列總長度</param> 6 /// <returns>空餘部分被填充預設值的新陣列</returns> 7 public static char[] ExtArrayFill(this char[] data, int defLength) 8 { 9 defLength = defLength - data.Length; 10 char[] dCharArray = new char[defLength]; 11 int i = 0; 12 while (i < defLength) 13 { 14 dCharArray[i++] = `