1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.Data; 7 using System.Diagnostics; 8 9 internal static class DataSetUtil 10 { 11 internal static void CheckArgumentNull<T>(T argumentValue, string argumentName) where T : class 12 { 13 if (null == argumentValue) 14 { 15 throw ArgumentNull(argumentName); 16 } 17 } 18 TraceException(string trace, T e)19 private static T TraceException<T>(string trace, T e) 20 { 21 Debug.Assert(null != e, "TraceException: null Exception"); 22 return e; 23 } 24 TraceExceptionAsReturnValue(T e)25 private static T TraceExceptionAsReturnValue<T>(T e) 26 { 27 return TraceException("<comm.ADP.TraceException|ERR|THROW> '%ls'\n", e); 28 } 29 Argument(string message)30 internal static ArgumentException Argument(string message) 31 { 32 return TraceExceptionAsReturnValue(new ArgumentException(message)); 33 } 34 ArgumentNull(string message)35 internal static ArgumentNullException ArgumentNull(string message) 36 { 37 return TraceExceptionAsReturnValue(new ArgumentNullException(message)); 38 } 39 ArgumentOutOfRange(string message, string parameterName)40 internal static ArgumentOutOfRangeException ArgumentOutOfRange(string message, string parameterName) 41 { 42 return TraceExceptionAsReturnValue(new ArgumentOutOfRangeException(parameterName, message)); 43 } 44 InvalidCast(string message)45 internal static InvalidCastException InvalidCast(string message) 46 { 47 return TraceExceptionAsReturnValue(new InvalidCastException(message)); 48 } 49 InvalidOperation(string message)50 internal static InvalidOperationException InvalidOperation(string message) 51 { 52 return TraceExceptionAsReturnValue(new InvalidOperationException(message)); 53 } 54 NotSupported(string message)55 internal static NotSupportedException NotSupported(string message) 56 { 57 return TraceExceptionAsReturnValue(new NotSupportedException(message)); 58 } 59 InvalidEnumerationValue(Type type, int value)60 static internal ArgumentOutOfRangeException InvalidEnumerationValue(Type type, int value) 61 { 62 return ArgumentOutOfRange(string.Format(SR.DataSetLinq_InvalidEnumerationValue, type.Name, value.ToString(System.Globalization.CultureInfo.InvariantCulture)), type.Name); 63 } 64 InvalidDataRowState(DataRowState value)65 static internal ArgumentOutOfRangeException InvalidDataRowState(DataRowState value) 66 { 67 #if DEBUG 68 switch (value) 69 { 70 case DataRowState.Detached: 71 case DataRowState.Unchanged: 72 case DataRowState.Added: 73 case DataRowState.Deleted: 74 case DataRowState.Modified: 75 Debug.Assert(false, "valid DataRowState " + value.ToString()); 76 break; 77 } 78 #endif 79 return InvalidEnumerationValue(typeof(DataRowState), (int)value); 80 } 81 InvalidLoadOption(LoadOption value)82 static internal ArgumentOutOfRangeException InvalidLoadOption(LoadOption value) 83 { 84 #if DEBUG 85 switch (value) 86 { 87 case LoadOption.OverwriteChanges: 88 case LoadOption.PreserveChanges: 89 case LoadOption.Upsert: 90 Debug.Assert(false, "valid LoadOption " + value.ToString()); 91 break; 92 } 93 #endif 94 return InvalidEnumerationValue(typeof(LoadOption), (int)value); 95 } 96 97 // only StackOverflowException & ThreadAbortException are sealed classes 98 private static readonly Type s_stackOverflowType = typeof(StackOverflowException); 99 private static readonly Type s_outOfMemoryType = typeof(OutOfMemoryException); 100 private static readonly Type s_threadAbortType = typeof(System.Threading.ThreadAbortException); 101 private static readonly Type s_nullReferenceType = typeof(NullReferenceException); 102 private static readonly Type s_accessViolationType = typeof(AccessViolationException); 103 private static readonly Type s_securityType = typeof(System.Security.SecurityException); 104 IsCatchableExceptionType(Exception e)105 static internal bool IsCatchableExceptionType(Exception e) 106 { 107 // a 'catchable' exception is defined by what it is not. 108 Type type = e.GetType(); 109 110 return ((type != s_stackOverflowType) && 111 (type != s_outOfMemoryType) && 112 (type != s_threadAbortType) && 113 (type != s_nullReferenceType) && 114 (type != s_accessViolationType) && 115 !s_securityType.IsAssignableFrom(type)); 116 } 117 }