Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data.DataSetExtensions / System / Data / DataSetUtil.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="DataSetUtil.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 //------------------------------------------------------------------------------
8
9 using System;
10 using System.Data;
11 using System.Data.DataSetExtensions;
12 using System.Diagnostics;
13
14 internal static class DataSetUtil
15 {
16     #region CheckArgument
17     internal static void CheckArgumentNull<T>(T argumentValue, string argumentName) where T : class
18     {
19         if (null == argumentValue)
20         {
21             throw ArgumentNull(argumentName);
22         }
23     }
24     #endregion
25
26     #region Trace
27     private static T TraceException<T>(string trace, T e)
28     {
29         Debug.Assert(null != e, "TraceException: null Exception");
30         if (null != e)
31         {
32             //Bid.Trace(trace, e.ToString()); // will include callstack if permission is available
33         }
34         return e;
35     }
36
37     private static T TraceExceptionAsReturnValue<T>(T e)
38     {
39         return TraceException("<comm.ADP.TraceException|ERR|THROW> '%ls'\n", e);
40     }
41     #endregion
42
43     #region new Exception
44     internal static ArgumentException Argument(string message)
45     {
46         return TraceExceptionAsReturnValue(new ArgumentException(message));
47     }
48
49     internal static ArgumentNullException ArgumentNull(string message)
50     {
51         return TraceExceptionAsReturnValue(new ArgumentNullException(message));
52     }
53
54     internal static ArgumentOutOfRangeException ArgumentOutOfRange(string message, string parameterName)
55     {
56         return TraceExceptionAsReturnValue(new ArgumentOutOfRangeException(parameterName, message));
57     }
58
59     internal static InvalidCastException InvalidCast(string message)
60     {
61         return TraceExceptionAsReturnValue(new InvalidCastException(message));
62     }
63
64     internal static InvalidOperationException InvalidOperation(string message)
65     {
66         return TraceExceptionAsReturnValue(new InvalidOperationException(message));
67     }
68
69     internal static NotSupportedException NotSupported(string message)
70     {
71         return TraceExceptionAsReturnValue(new NotSupportedException(message));
72     }
73     #endregion
74
75     #region new EnumerationValueNotValid
76     static internal ArgumentOutOfRangeException InvalidEnumerationValue(Type type, int value)
77     {
78         return ArgumentOutOfRange(Strings.DataSetLinq_InvalidEnumerationValue(type.Name, value.ToString(System.Globalization.CultureInfo.InvariantCulture)), type.Name);
79     }
80
81     static internal ArgumentOutOfRangeException InvalidDataRowState(DataRowState value)
82     {
83 #if DEBUG
84         switch (value)
85         {
86             case DataRowState.Detached:
87             case DataRowState.Unchanged:
88             case DataRowState.Added:
89             case DataRowState.Deleted:
90             case DataRowState.Modified:
91                 Debug.Assert(false, "valid DataRowState " + value.ToString());
92                 break;
93         }
94 #endif
95         return InvalidEnumerationValue(typeof(DataRowState), (int)value);
96     }
97
98     static internal ArgumentOutOfRangeException InvalidLoadOption(LoadOption value)
99     {
100 #if DEBUG
101         switch (value)
102         {
103             case LoadOption.OverwriteChanges:
104             case LoadOption.PreserveChanges:
105             case LoadOption.Upsert:
106                 Debug.Assert(false, "valid LoadOption " + value.ToString());
107                 break;
108         }
109 #endif
110         return InvalidEnumerationValue(typeof(LoadOption), (int)value);
111     }
112     #endregion
113
114     // only StackOverflowException & ThreadAbortException are sealed classes
115     static private readonly Type StackOverflowType = typeof(System.StackOverflowException);
116     static private readonly Type OutOfMemoryType = typeof(System.OutOfMemoryException);
117     static private readonly Type ThreadAbortType = typeof(System.Threading.ThreadAbortException);
118     static private readonly Type NullReferenceType = typeof(System.NullReferenceException);
119     static private readonly Type AccessViolationType = typeof(System.AccessViolationException);
120     static private readonly Type SecurityType = typeof(System.Security.SecurityException);
121
122     static internal bool IsCatchableExceptionType(Exception e)
123     {
124         // a 'catchable' exception is defined by what it is not.
125         Type type = e.GetType();
126
127         return ((type != StackOverflowType) &&
128                  (type != OutOfMemoryType) &&
129                  (type != ThreadAbortType) &&
130                  (type != NullReferenceType) &&
131                  (type != AccessViolationType) &&
132                  !SecurityType.IsAssignableFrom(type));
133     }
134 }