public static Exception PrepareForRethrow(this Exception exception) { Fx.Assert(exception != null, "The specified Exception is null."); if (!ShouldPrepareForRethrow(exception)) { return(exception); } if (PartialTrustHelpers.UnsafeIsInFullTrust()) { // Racing here is harmless if (ExceptionExtensions.prepForRemotingMethodInfo == null) { ExceptionExtensions.prepForRemotingMethodInfo = typeof(Exception).GetMethod("PrepForRemoting", BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { }, new ParameterModifier[] { }); } if (ExceptionExtensions.prepForRemotingMethodInfo != null) { // PrepForRemoting is not thread-safe. When the same exception instance is thrown by multiple threads // the remote stack trace string may not format correctly. However, We don't lock this to protect us from it given // it is discouraged to throw the same exception instance from multiple threads and the side impact is ignorable. prepForRemotingMethodInfo.Invoke(exception, new object[] { }); } } return(exception); }