1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
6 * copy of the license can be found in the License.html file at the root of this distribution. If
7 * you cannot locate the Apache License, Version 2.0, please send an email to
8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
9 * by the terms of the Apache License, Version 2.0.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
18 using System.Runtime.CompilerServices;
19 using System.Collections.Generic;
20 using System.Threading;
22 namespace Microsoft.Scripting.Utils {
23 public static class ExceptionUtils {
24 public static ArgumentOutOfRangeException MakeArgumentOutOfRangeException(string paramName, object actualValue, string message) {
25 #if SILVERLIGHT || WP75 // ArgumentOutOfRangeException ctor overload
26 throw new ArgumentOutOfRangeException(paramName, string.Format("{0} (actual value is '{1}')", message, actualValue));
28 throw new ArgumentOutOfRangeException(paramName, actualValue, message);
32 public static ArgumentNullException MakeArgumentItemNullException(int index, string arrayName) {
33 return new ArgumentNullException(String.Format("{0}[{1}]", arrayName, index));
37 public static object GetData(this Exception e, object key) {
41 public static void SetData(this Exception e, object key, object data) {
45 public static void RemoveData(this Exception e, object key) {
51 private static WeakDictionary<Exception, List<KeyValuePair<object, object>>> _exceptionData;
53 private static ConditionalWeakTable<Exception, List<KeyValuePair<object, object>>> _exceptionData;
56 public static void SetData(this Exception e, object key, object value) {
57 if (_exceptionData == null) {
59 Interlocked.CompareExchange(ref _exceptionData, new WeakDictionary<Exception, List<KeyValuePair<object, object>>>(), null);
61 Interlocked.CompareExchange(ref _exceptionData, new ConditionalWeakTable<Exception, List<KeyValuePair<object, object>>>(), null);
65 lock (_exceptionData) {
66 var data = _exceptionData.GetOrCreateValue(e);
68 int index = data.FindIndex(entry => entry.Key == key);
70 data[index] = new KeyValuePair<object, object>(key, value);
72 data.Add(new KeyValuePair<object, object>(key, value));
77 public static object GetData(this Exception e, object key) {
78 if (_exceptionData == null) {
82 lock (_exceptionData) {
83 List<KeyValuePair<object, object>> data;
84 if (!_exceptionData.TryGetValue(e, out data)) {
88 return data.FirstOrDefault(entry => entry.Key == key).Value;
92 public static void RemoveData(this Exception e, object key) {
93 if (_exceptionData == null) {
97 lock (_exceptionData) {
98 List<KeyValuePair<object, object>> data;
99 if (!_exceptionData.TryGetValue(e, out data)) {
103 int index = data.FindIndex(entry => entry.Key == key);
105 data.RemoveAt(index);