Posts
[Extension Method]使用擴充方法來做物件的深層複製
Public Module ObjectExtension
#Region “Const” Const BUFFER_SIZE As Integer = 512 #End Region
#Region “Public Method”
<Extension()> _ Public Function Clone(Of T)(ByVal obj As T, Optional ByVal serializeType As SerializationFormat = SerializationFormat.Binary) As T Select Case serializeType Case SerializationFormat.Binary Dim br As New BinaryFormatter() Using ms As New MemoryStream(BUFFER_SIZE) br.Serialize(ms, obj) ms.Seek(0, SeekOrigin.Begin) Return DirectCast(br.Deserialize(ms), T) End Using Case SerializationFormat.Xml Dim x As New XmlSerializer(obj.GetType) Using ms As New MemoryStream(BUFFER_SIZE) x.
read morePosts
[Extension Method]使用擴充方法來做二維陣列排序
T[] tempArray = new T[count]; for (int idx = 0; idx < count; ++idx) { tempArray[idx] = array[idx, d2Idx]; } Array.Sort(tempArray); for (int idx = 0; idx < count; ++idx) { T tempValue = tempArray[idx]; for (int idx2 = idx+1; idx2 < count; ++idx2) { if (array[idx2, d2Idx].Equals(tempValue)) { array.Swap(idx, d2Idx, idx2, d2Idx); int d2Idx2 = (d2Idx == 0) ? 1 : 0; array.Swap(idx, d2Idx2, idx2, d2Idx2); } } } } public static void Swap<T>(this T[,] array, int idx1, int idx2, int targetIdx1, int targetIdx2) { T temp; temp = array[targetIdx1, targetIdx2]; array[targetIdx1, targetIdx2] = array[idx1, idx2]; array[idx1, idx2] = temp; } 使用上呼叫Sort方法,並傳入要排序依據的索引即可。
read morePosts
[C#]用Stopwatch計算累計耗費時間的注意事項
for (int i = 1; i <= count; i++) { sw.Reset(); sw.Start(); string guid = Guid.NewGuid().ToString(); sw.Stop(); total += sw.ElapsedMilliseconds; } Console.WriteLine(total); sw.Reset(); for (int i = 1; i <= count; i++) { sw.Start(); string guid = Guid.NewGuid().ToString(); sw.Stop(); } Console.WriteLine(sw.ElapsedMilliseconds); }</pre></div> for (int i = 1; i <= count; i++) { sw.Reset(); sw.Start(); System.Threading.Thread.Sleep(10); sw.Stop(); total += sw.ElapsedMilliseconds; } Console.WriteLine(total); sw.Reset(); for (int i = 1; i <= count; i++) { sw.
read morePosts
[VB.NET]用FindExecutable API取得開啟文件用的執行檔位置
<td valign="top" width="200">指定的檔案不存在</td> </tr> <tr> <td valign="top" width="200">SE_ERR_NOASSOC (31)</td> <td valign="top" width="200">沒有對應用來開啟的執行檔</td> </tr> <tr> <td valign="top" width="200">SE_ERR_OOM (8)</td> <td valign="top" width="200">Windows XP only. 系統記憶體資源不足</td> </tr> Public Class NoAssociatedFileTypeException Inherits ApplicationException
End Class
Public Class Win32API Const SE_ERR_FNF As Integer = 2 Const SE_ERR_OOM As Integer = 8 Const SE_ERR_NOASSOC As Integer = 31
<Runtime.InteropServices.DllImport("shell32.DLL", EntryPoint:="FindExecutable")> _ Private Shared Function FindExecutable(ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As StringBuilder) As Integer End Function
read more