21 [__DynamicallyInvokable]
25 internal class __RestrictedErrorObject
28 private object _realErrorObject;
30 public object RealErrorObject => _realErrorObject;
32 internal __RestrictedErrorObject(
object errorObject)
34 _realErrorObject = errorObject;
38 internal enum ExceptionMessageKind
46 private static object s_EDILock =
new object();
48 private string _className;
52 private string _exceptionMethodString;
54 internal string _message;
60 private string _helpURL;
62 private object _stackTrace;
65 private object _watsonBuckets;
67 private string _stackTraceString;
69 private string _remoteStackTraceString;
71 private int _remoteStackIndex;
73 private object _dynamicMethods;
75 internal int _HResult;
77 private string _source;
84 private UIntPtr _ipForWatsonBuckets;
86 [OptionalField(VersionAdded = 4)]
87 private SafeSerializationManager _safeSerializationManager;
89 private const int _COMPlusExceptionCode = -532462766;
93 [__DynamicallyInvokable]
96 [__DynamicallyInvokable]
101 if (_className ==
null)
103 _className = GetClassName();
105 return Environment.GetResourceString(
"Exception_WasThrown", _className);
113 [__DynamicallyInvokable]
116 [SecuritySafeCritical]
117 [__DynamicallyInvokable]
122 if (IsImmutableAgileException(
this))
124 _data =
new EmptyReadOnlyDictionaryInternal();
128 _data =
new ListDictionaryInternal();
137 [__DynamicallyInvokable]
140 [__DynamicallyInvokable]
143 return _innerException;
151 [SecuritySafeCritical]
154 return GetTargetSiteInternal();
160 [__DynamicallyInvokable]
163 [__DynamicallyInvokable]
166 return GetStackTrace(needFileInfo:
true);
172 [__DynamicallyInvokable]
175 [__DynamicallyInvokable]
180 [__DynamicallyInvokable]
190 [__DynamicallyInvokable]
191 public virtual string Source 193 [__DynamicallyInvokable]
204 RuntimeModule runtimeModule = module as RuntimeModule;
205 if (runtimeModule ==
null)
208 if (!(moduleBuilder !=
null))
212 runtimeModule = moduleBuilder.InternalModule;
214 _source = runtimeModule.GetRuntimeAssembly().GetSimpleName();
219 [__DynamicallyInvokable]
226 internal UIntPtr IPForWatsonBuckets => _ipForWatsonBuckets;
228 internal object WatsonBuckets => _watsonBuckets;
230 internal string RemoteStackTrace => _remoteStackTraceString;
234 [__DynamicallyInvokable]
237 [__DynamicallyInvokable]
242 [__DynamicallyInvokable]
249 internal bool IsTransient
251 [SecuritySafeCritical]
254 return nIsTransient(_HResult);
263 _safeSerializationManager.SerializeObjectState += value;
267 _safeSerializationManager.SerializeObjectState -= value;
275 _dynamicMethods =
null;
279 _watsonBuckets =
null;
281 _safeSerializationManager =
new SafeSerializationManager();
285 [__DynamicallyInvokable]
293 [__DynamicallyInvokable]
303 [__DynamicallyInvokable]
308 _innerException = innerException;
316 [SecuritySafeCritical]
323 _className = info.GetString(
"ClassName");
324 _message = info.GetString(
"Message");
327 _helpURL = info.GetString(
"HelpURL");
328 _stackTraceString = info.GetString(
"StackTraceString");
329 _remoteStackTraceString = info.GetString(
"RemoteStackTraceString");
330 _remoteStackIndex = info.GetInt32(
"RemoteStackIndex");
331 _exceptionMethodString = (string)info.GetValue(
"ExceptionMethod", typeof(
string));
332 HResult = info.GetInt32(
"HResult");
333 _source = info.GetString(
"Source");
334 _watsonBuckets = info.GetValueNoThrow(
"WatsonBuckets", typeof(
byte[]));
335 _safeSerializationManager = (info.GetValueNoThrow(
"SafeSerializationManager", typeof(SafeSerializationManager)) as SafeSerializationManager);
336 if (_className ==
null ||
HResult == 0)
342 _remoteStackTraceString += _stackTraceString;
343 _stackTraceString =
null;
349 private static extern bool IsImmutableAgileException(
Exception e);
351 [FriendAccessAllowed]
352 internal void AddExceptionDataForRestrictedErrorInfo(
string restrictedError,
string restrictedErrorReference,
string restrictedCapabilitySid,
object restrictedErrorObject,
bool hasrestrictedLanguageErrorObject =
false)
357 data.Add(
"RestrictedDescription", restrictedError);
358 data.Add(
"RestrictedErrorReference", restrictedErrorReference);
359 data.Add(
"RestrictedCapabilitySid", restrictedCapabilitySid);
360 data.Add(
"__RestrictedErrorObject", (restrictedErrorObject ==
null) ?
null :
new __RestrictedErrorObject(restrictedErrorObject));
361 data.Add(
"__HasRestrictedLanguageErrorObject", hasrestrictedLanguageErrorObject);
365 internal bool TryGetRestrictedLanguageErrorObject(out
object restrictedErrorObject)
367 restrictedErrorObject =
null;
372 __RestrictedErrorObject _RestrictedErrorObject =
Data[
"__RestrictedErrorObject"] as __RestrictedErrorObject;
373 if (_RestrictedErrorObject !=
null)
375 restrictedErrorObject = _RestrictedErrorObject.RealErrorObject;
378 return (
bool)
Data[
"__HasRestrictedLanguageErrorObject"];
383 private string GetClassName()
385 if (_className ==
null)
394 [__DynamicallyInvokable]
399 while (innerException !=
null)
401 result = innerException;
409 private static extern IRuntimeMethodInfo GetMethodFromStackTrace(
object stackTrace);
411 [SecuritySafeCritical]
412 private MethodBase GetExceptionMethodFromStackTrace()
414 IRuntimeMethodInfo methodFromStackTrace = GetMethodFromStackTrace(_stackTrace);
415 if (methodFromStackTrace ==
null)
419 return RuntimeType.GetMethodBase(methodFromStackTrace);
425 if (_exceptionMethod !=
null)
427 return _exceptionMethod;
429 if (_stackTrace ==
null)
433 if (_exceptionMethodString !=
null)
435 _exceptionMethod = GetExceptionMethodFromString();
439 _exceptionMethod = GetExceptionMethodFromStackTrace();
441 return _exceptionMethod;
444 private string GetStackTrace(
bool needFileInfo)
446 string text = _stackTraceString;
447 string text2 = _remoteStackTraceString;
450 text = StripFileInfo(text, isRemoteStackTrace:
false);
451 text2 = StripFileInfo(text2, isRemoteStackTrace:
true);
457 if (_stackTrace ==
null)
461 string stackTrace = Environment.GetStackTrace(
this, needFileInfo);
462 return text2 + stackTrace;
465 [FriendAccessAllowed]
466 internal void SetErrorCode(
int hr)
473 [__DynamicallyInvokable]
476 return ToString(needFileLineInfo:
true, needMessage:
true);
479 private string ToString(
bool needFileLineInfo,
bool needMessage)
481 string text = needMessage ?
Message :
null;
482 string text2 = (text !=
null && text.Length > 0) ? (GetClassName() +
": " + text) : GetClassName();
483 if (_innerException !=
null)
485 text2 = text2 +
" ---> " + _innerException.
ToString(needFileLineInfo, needMessage) +
Environment.
NewLine +
" " +
Environment.GetResourceString(
"Exception_EndOfInnerExceptionStack");
487 string stackTrace = GetStackTrace(needFileLineInfo);
488 if (stackTrace !=
null)
490 text2 = text2 + Environment.NewLine + stackTrace;
496 private string GetExceptionMethodString()
498 MethodBase targetSiteInternal = GetTargetSiteInternal();
499 if (targetSiteInternal ==
null)
511 RuntimeConstructorInfo runtimeConstructorInfo = (RuntimeConstructorInfo)targetSiteInternal;
512 Type reflectedType = runtimeConstructorInfo.ReflectedType;
514 stringBuilder.
Append(value);
515 stringBuilder.
Append(runtimeConstructorInfo.Name);
516 if (reflectedType !=
null)
518 stringBuilder.
Append(value);
519 stringBuilder.
Append(reflectedType.Assembly.FullName);
520 stringBuilder.
Append(value);
521 stringBuilder.
Append(reflectedType.FullName);
523 stringBuilder.
Append(value);
524 stringBuilder.
Append(runtimeConstructorInfo.ToString());
528 RuntimeMethodInfo runtimeMethodInfo = (RuntimeMethodInfo)targetSiteInternal;
529 Type declaringType = runtimeMethodInfo.DeclaringType;
531 stringBuilder.
Append(value);
532 stringBuilder.
Append(runtimeMethodInfo.Name);
533 stringBuilder.
Append(value);
534 stringBuilder.
Append(runtimeMethodInfo.Module.Assembly.FullName);
535 stringBuilder.
Append(value);
536 if (declaringType !=
null)
538 stringBuilder.
Append(declaringType.FullName);
539 stringBuilder.
Append(value);
541 stringBuilder.
Append(runtimeMethodInfo.ToString());
547 private MethodBase GetExceptionMethodFromString()
549 string[] array = _exceptionMethodString.Split(
'\0',
'\n');
550 if (array.Length != 5)
556 serializationInfo.
AddValue(
"Name", array[1], typeof(
string));
557 serializationInfo.
AddValue(
"AssemblyName", array[2], typeof(
string));
558 serializationInfo.
AddValue(
"ClassName", array[3]);
559 serializationInfo.
AddValue(
"Signature", array[4]);
563 return (
MethodBase)
new MemberInfoSerializationHolder(serializationInfo, context).GetRealObject(context);
582 string text = _stackTraceString;
583 if (_stackTrace !=
null)
587 text =
Environment.GetStackTrace(
this, needFileInfo:
true);
589 if (_exceptionMethod ==
null)
591 _exceptionMethod = GetExceptionMethodFromStackTrace();
598 info.AddValue(
"ClassName", GetClassName(), typeof(
string));
599 info.AddValue(
"Message", _message, typeof(
string));
601 info.AddValue(
"InnerException", _innerException, typeof(
Exception));
602 info.AddValue(
"HelpURL", _helpURL, typeof(
string));
603 info.AddValue(
"StackTraceString", text, typeof(
string));
604 info.AddValue(
"RemoteStackTraceString", _remoteStackTraceString, typeof(
string));
605 info.AddValue(
"RemoteStackIndex", _remoteStackIndex, typeof(
int));
606 info.AddValue(
"ExceptionMethod", GetExceptionMethodString(), typeof(
string));
607 info.AddValue(
"HResult",
HResult);
608 info.AddValue(
"Source", _source, typeof(
string));
609 info.AddValue(
"WatsonBuckets", _watsonBuckets, typeof(
byte[]));
610 if (_safeSerializationManager !=
null && _safeSerializationManager.IsActive)
612 info.AddValue(
"SafeSerializationManager", _safeSerializationManager, typeof(SafeSerializationManager));
613 _safeSerializationManager.CompleteSerialization(
this, info, context);
629 _ipForWatsonBuckets = UIntPtr.
Zero;
630 if (_safeSerializationManager ==
null)
632 _safeSerializationManager =
new SafeSerializationManager();
636 _safeSerializationManager.CompleteDeserialization(
this);
640 internal void InternalPreserveStackTrace()
645 stackTrace = GetStackTrace(needFileInfo:
true);
652 if (stackTrace !=
null && stackTrace.Length > 0)
654 _remoteStackTraceString = stackTrace + Environment.NewLine;
657 _stackTraceString =
null;
662 private static extern void PrepareForForeignExceptionRaise();
666 private static extern void GetStackTracesDeepCopy(
Exception exception, out
object currentStackTrace, out
object dynamicMethodArray);
670 internal static extern void SaveStackTracesFromDeepCopy(
Exception exception,
object currentStackTrace,
object dynamicMethodArray);
674 private static extern object CopyStackTrace(
object currentStackTrace);
678 private static extern object CopyDynamicMethods(
object currentDynamicMethods);
681 [SecuritySafeCritical]
682 private extern string StripFileInfo(
string stackTrace,
bool isRemoteStackTrace);
684 [SecuritySafeCritical]
685 internal object DeepCopyStackTrace(
object currentStackTrace)
687 if (currentStackTrace !=
null)
689 return CopyStackTrace(currentStackTrace);
694 [SecuritySafeCritical]
695 internal object DeepCopyDynamicMethods(
object currentDynamicMethods)
697 if (currentDynamicMethods !=
null)
699 return CopyDynamicMethods(currentDynamicMethods);
704 [SecuritySafeCritical]
705 internal void GetStackTracesDeepCopy(out
object currentStackTrace, out
object dynamicMethodArray)
707 GetStackTracesDeepCopy(
this, out currentStackTrace, out dynamicMethodArray);
710 [SecuritySafeCritical]
713 if (!IsImmutableAgileException(
this))
720 object currentStackTrace = (exceptionDispatchInfo.BinaryStackTraceArray ==
null) ?
null : DeepCopyStackTrace(exceptionDispatchInfo.BinaryStackTraceArray);
721 object dynamicMethodArray = (exceptionDispatchInfo.DynamicMethodArray ==
null) ?
null : DeepCopyDynamicMethods(exceptionDispatchInfo.DynamicMethodArray);
724 _watsonBuckets = exceptionDispatchInfo.WatsonBuckets;
725 _ipForWatsonBuckets = exceptionDispatchInfo.IPForWatsonBuckets;
726 _remoteStackTraceString = exceptionDispatchInfo.RemoteStackTrace;
727 SaveStackTracesFromDeepCopy(
this, currentStackTrace, dynamicMethodArray);
729 _stackTraceString =
null;
730 PrepareForForeignExceptionRaise();
736 internal virtual string InternalToString()
741 securityPermission.
Assert();
746 bool needFileLineInfo =
true;
747 return ToString(needFileLineInfo, needMessage:
true);
752 [__DynamicallyInvokable]
760 private static extern bool nIsTransient(
int hr);
762 [SecuritySafeCritical]
763 internal static string GetMessageFromNativeResources(ExceptionMessageKind kind)
766 GetMessageFromNativeResources(kind, JitHelpers.GetStringHandleOnStack(ref s));
772 [SuppressUnmanagedCodeSecurity]
773 private static extern void GetMessageFromNativeResources(ExceptionMessageKind kind, StringHandleOnStack retMesg);
override string ToString()
Creates and returns a string representation of the current exception.
A platform-specific type that is used to represent a pointer or a handle.
Performs reflection on a module.
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Exception(string message)
Initializes a new instance of the T:System.Exception class with a specified error message.
static string NewLine
Gets the newline string defined for this environment.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
Defines and represents a dynamic method that can be compiled, executed, and discarded....
StreamingContextStates State
Gets the source or destination of the transmitted data.
bool Contains(object key)
Determines whether the T:System.Collections.IDictionary object contains an element with the specified...
virtual void GetObjectData(SerializationInfo info, StreamingContext context)
When overridden in a derived class, sets the T:System.Runtime.Serialization.SerializationInfo with in...
MethodBase TargetSite
Gets the method that throws the current exception.
Discovers the attributes of a class constructor and provides access to constructor metadata.
Represents a stack trace, which is an ordered collection of one or more stack frames.
EventHandler< SafeSerializationEventArgs > SerializeObjectState
Occurs when an exception is serialized to create an exception state object that contains serialized d...
virtual int FrameCount
Gets the number of frames in the stack trace.
new Type GetType()
Gets the runtime type of the current instance.
override string ToString()
Returns a String representing the name of the current Type.
virtual string Source
Gets or sets the name of the application or the object that causes the error.
virtual string HelpLink
Gets or sets a link to the help file associated with this exception.
Describes the source and destination of a given serialized stream, and provides an additional caller-...
Represents an exception whose state is captured at a certain point in code.
Provides information about, and means to manipulate, the current environment and platform....
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
void AddValue(string name, object value, Type type)
Adds a value into the T:System.Runtime.Serialization.SerializationInfo store, where value is associa...
int HResult
Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.
Finds or creates a T:System.AppDomain.
Exception(string message, Exception innerException)
Initializes a new instance of the T:System.Exception class with a specified error message and a refer...
Defines and represents a module in a dynamic assembly.
virtual string StackTrace
Gets a string representation of the immediate frames on the call stack.
Exception InnerException
Gets the T:System.Exception instance that caused the current exception.
A platform-specific type that is used to represent a pointer or a handle.
Represents type declarations: class types, interface types, array types, value types,...
virtual MethodBase GetMethod()
Gets the method in which the frame is executing.
Provides information about methods and constructors.
The exception thrown when an error occurs during serialization or deserialization.
MethodImplOptions
Defines the details of how a method is implemented.
CharSet
Dictates which character set marshaled strings should use.
virtual string Message
Gets a message that describes the current exception.
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
Exception()
Initializes a new instance of the T:System.Exception class.
The exception that is thrown when one of the arguments provided to a method is not valid.
Allows an object to control its own serialization and deserialization.
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
virtual Exception GetBaseException()
When overridden in a derived class, returns the T:System.Exception that is the root cause of one or m...
virtual Module Module
Gets the module in which the type that declares the member represented by the current T:System....
Exception(SerializationInfo info, StreamingContext context)
Initializes a new instance of the T:System.Exception class with serialized data.
Specifies that the class can be serialized.
static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Gets the T:System.Type with the specified name, specifying whether to throw an exception if the type ...
ClassInterfaceType
Identifies the type of class interface that is generated for a class.
Provides information about a T:System.Diagnostics.StackFrame, which represents a function call on the...
void Assert()
Declares that the calling code can access the resource protected by a permission demand through the c...
StreamingContextStates
Defines a set of flags that specifies the source or destination context for the stream during seriali...
Provides information about a specific culture (called a locale for unmanaged code development)....
static readonly UIntPtr Zero
A read-only field that represents a pointer or handle that has been initialized to zero.
SecurityPermissionFlag
Specifies access flags for the security permission object.
Represents a nongeneric collection of key/value pairs.
virtual IDictionary Data
Gets a collection of key/value pairs that provide additional user-defined information about the excep...
virtual StackFrame GetFrame(int index)
Gets the specified stack frame.
Exposes the public members of the T:System.Exception class to unmanaged code.