Create a gist now

Instantly share code, notes, and snippets.

anonymous /gist:3819116
Created Oct 2, 2012

What would you like to do?
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using NDepend.Attributes;
using ThreadState = System.Threading.ThreadState;
namespace NDepend.Helpers.Collections {
//
// These extension methods are paliative to a bug of the .NET Fx described here:
// http://connect.microsoft.com/VisualStudio/feedback/details/605536/list-sort-throws-invalidoperationexception-instead-of-re-throw-threadabortexception-while-thread-is-being-aborted
// List.Sort() throws InvalidOperationException (instead of re-throw ThreadAbortException) while thread is being aborted
//
#if DEBUG
[CodeRule(@"// <Name>Use List<T>.BugFreeSort() instead of List<T>.Sort()</Name>
warnif count > 0
let listSortMethods = ThirdParty.Methods
.WithFullNameWildcardMatch(@""System.Collections.Generic.List<T>.Sort*"")
from m in Application.Methods.UsingAny(listSortMethods)
where m.ParentType.FullName != ""NDepend.Helpers.Collections.ListSortExtensionMethods""
select new { m, sortMethodCalled = m.MethodsCalled.Intersect(listSortMethods) }",
Active = true,
DisplayStatInReport = true,
DisplayListInReport = true,
DisplaySelectionViewInReport = true,
IsCriticalRule = false)]
#endif
public static class ListSortExtensionMethods {
public static void BugFreeSort<T>(this List<T> list) {
Debug.Assert(list != null);
try {
list.Sort();
} catch(InvalidOperationException) {
Debug.Assert((Thread.CurrentThread.ThreadState & ThreadState.AbortRequested) != 0);
// The exception ThreadAbortException is propagated when closing this catch blok
}
}
public static void BugFreeSort<T>(this List<T> list, Comparison<T> comparison) {
Debug.Assert(list != null);
Debug.Assert(comparison != null);
try {
list.Sort(comparison);
} catch (InvalidOperationException) {
Debug.Assert((Thread.CurrentThread.ThreadState & ThreadState.AbortRequested) != 0);
// The exception ThreadAbortException is propagated when closing this catch blok
}
}
public static void BugFreeSort<T>(this List<T> list, IComparer<T> comparer) {
Debug.Assert(list != null);
Debug.Assert(comparer != null);
try {
list.Sort(comparer);
} catch (InvalidOperationException) {
Debug.Assert((Thread.CurrentThread.ThreadState & ThreadState.AbortRequested) != 0);
// The exception ThreadAbortException is propagated when closing this catch blok
}
}
public static void BugFreeSort<T>(this List<T> list, int index, int count, IComparer<T> comparer) {
Debug.Assert(list != null);
Debug.Assert(index >= 0);
Debug.Assert(count >=0);
Debug.Assert(list.Count == 0 || index+count < list.Count);
Debug.Assert(comparer != null);
try {
list.Sort(index, count, comparer);
} catch (InvalidOperationException) {
Debug.Assert((Thread.CurrentThread.ThreadState & ThreadState.AbortRequested) != 0);
// The exception ThreadAbortException is propagated when closing this catch blok
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment