/// <summary> /// Custom helper to deserialize a SqlException object from Xml. /// </summary> private static SqlException DeserializeSqlException(System.Xml.XmlReader reader) { // SqlException constructor takes in two parameters, an errorCollection and a serverVersion. SqlErrorCollection errorCollection = (SqlErrorCollection)typeof(SqlErrorCollection).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, System.Type.EmptyTypes, null).Invoke(null);; string serverVersion = null; // Read the subtree and fill in the parameters. int startDepth = reader.Depth; reader.ReadStartElement(); while (reader.Depth > startDepth) { switch (reader.Name) { case "serverVersion": serverVersion = reader.ReadElementContentAsString(); break; case "SqlError": SqlError newSqlError = DeserializeSqlError(reader); errorCollection.GetType().GetMethod("Add", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(errorCollection, new object[] { newSqlError }); break; } } reader.ReadEndElement(); // Use reflection to create the SqlException. Type sqlExceptionType = typeof(SqlException); Type[] types = { typeof(SqlErrorCollection), typeof(String) }; MethodInfo info = sqlExceptionType.GetMethod("CreateException", BindingFlags.Static | BindingFlags.NonPublic, null, types, null); return((SqlException)info.Invoke(null, new object[] { errorCollection, serverVersion })); }
public static SqlException CreateSqlException(string errorMessage, int errorNumber, int state = 0, int severity = 10, int lineNumber = 0, string server = "ServiceVirt", string procedureName = "ServiceVirtUnknownProcName") { SqlErrorCollection collection = GetErrorCollection(); SqlError error = GetError(errorNumber, errorMessage, state, severity, lineNumber, server, procedureName); MethodInfo addMethod = collection.GetType().GetMethod("Add", BindingFlags.NonPublic | BindingFlags.Instance); addMethod.Invoke(collection, new object[] { error }); Type[] types = new Type[] { typeof(string), typeof(SqlErrorCollection), typeof(Exception), typeof(Guid) }; object[] parameters = new object[] { errorMessage, collection, null, Guid.NewGuid() }; ConstructorInfo constructor = typeof(SqlException).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, types, null); SqlException exception = (SqlException)constructor.Invoke(parameters); return(exception); }
public static SqlException CreateSqlException(string errorMessage, int errorNumber) { SqlErrorCollection collection = GetErrorCollection(); SqlError error = GetError(errorNumber, errorMessage); MethodInfo addMethod = collection.GetType(). GetMethod("Add", BindingFlags.NonPublic | BindingFlags.Instance); addMethod.Invoke(collection, new object[] { error }); Type[] types = new Type[] { typeof(string), typeof(SqlErrorCollection) }; object[] parameters = new object[] { errorMessage, collection }; ConstructorInfo constructor = typeof(SqlException). GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, types, null); SqlException exception = (SqlException)constructor.Invoke(parameters); return(exception); }