Microsoft.Bcl.Async中是否存在ExceptionDispatchInfo的模拟?

Microsoft.Bcl.Async中是否存在ExceptionDispatchInfo的模拟? 我找不到类似的东西。

这个问题是由我的另一个问题引发的。 当exception的父task可用时,我可以使用task.GetAwaiter().GetResult()来重新抛出,如@StephenCleary所建议的那样。

当它不可用时,我有哪些选择?

我已经尝试了由avo提供的实现 – 但它没有通过所有的测试。 我的意思是它提供了基本行为,但在更复杂的情况下会失败(例如多次投掷,重新捕获等等)。

作为Theraot.Core的一部分,我以这样的方式创建了这个类的后端端口,它可以在现代Mono,旧Mono(2.6 *之前)和任何Microsoft .NET 2.0到4.0中工作。

*:来自Miguel de Icaza的小小提示:)

下面的代码目前仅在Feature分支上,它最终将移动到master分支(以及Task for .NET 2.0 :),此时它将通过Theraot.Core nuget提供 。 我将它留在这里以防万一有人需要它。

如果您发现任何问题,欢迎在项目的github(上面链接)上的问题跟踪器上提供错误报告。

 #if NET20 || NET30 || NET35 || NET40 using System.Reflection; namespace System.Runtime.ExceptionServices { ///  /// The ExceptionDispatchInfo object stores the stack trace information and Watson information that the exception contains at the point where it is captured. The exception can be thrown at another time and possibly on another thread by calling the ExceptionDispatchInfo.Throw method. The exception is thrown as if it had flowed from the point where it was captured to the point where the Throw method is called. ///  public sealed class ExceptionDispatchInfo { private static FieldInfo _remoteStackTraceString; private Exception _exception; private object _stackTraceOriginal; private object _stackTrace; private ExceptionDispatchInfo(Exception exception) { _exception = exception; _stackTraceOriginal = _exception.StackTrace; _stackTrace = _exception.StackTrace; if (_stackTrace != null) { _stackTrace += Environment.NewLine + "---End of stack trace from previous location where exception was thrown ---" + Environment.NewLine; } else { _stackTrace = string.Empty; } } ///  /// Creates an ExceptionDispatchInfo object that represents the specified exception at the current point in code. ///  /// The exception whose state is captured, and which is represented by the returned object. /// An object that represents the specified exception at the current point in code.  public static ExceptionDispatchInfo Capture(Exception source) { if (source == null) { throw new ArgumentNullException("source"); } return new ExceptionDispatchInfo(source); } ///  /// Gets the exception that is represented by the current instance. ///  public Exception SourceException { get { return _exception; } } private static FieldInfo GetFieldInfo() { if (_remoteStackTraceString == null) { // --- // Code by Miguel de Icaza FieldInfo remoteStackTraceString = typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.Instance | BindingFlags.NonPublic); // MS.Net if (remoteStackTraceString == null) remoteStackTraceString = typeof(Exception).GetField("remote_stack_trace", BindingFlags.Instance | BindingFlags.NonPublic); // Mono pre-2.6 // --- _remoteStackTraceString = remoteStackTraceString; } return _remoteStackTraceString; } private static void SetStackTrace(Exception exception, object value) { FieldInfo remoteStackTraceString = GetFieldInfo(); remoteStackTraceString.SetValue(exception, value); } ///  /// Throws the exception that is represented by the current ExceptionDispatchInfo object, after restoring the state that was saved when the exception was captured. ///  public void Throw() { try { throw _exception; } catch (Exception exception) { GC.KeepAlive(exception); var newStackTrace = _stackTrace + BuildStackTrace(Environment.StackTrace); SetStackTrace(_exception, newStackTrace); throw; } } private string BuildStackTrace(string trace) { var items = trace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); var newStackTrace = new Text.StringBuilder(); var found = false; foreach (var item in items) { // Only include lines that has files in the source code if (item.Contains(":")) { if (item.Contains("System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()")) { // Stacktrace from here on will be added by the CLR break; } if (found) { newStackTrace.Append(Environment.NewLine); } found = true; newStackTrace.Append(item); } else if (found) { break; } } var result = newStackTrace.ToString(); return result; } } } #endif 

这是Mono的ExceptionDispatchInfo的实现。 就我测试而言,它似乎与Microsoft .NET 4.0兼容。

 public sealed class ExceptionDispatchInfo { readonly Exception _exception; readonly object _source; readonly string _stackTrace; const BindingFlags PrivateInstance = BindingFlags.Instance | BindingFlags.NonPublic; static readonly FieldInfo RemoteStackTrace = typeof(Exception).GetField("_remoteStackTraceString", PrivateInstance); static readonly FieldInfo Source = typeof(Exception).GetField("_source", PrivateInstance); static readonly MethodInfo InternalPreserveStackTrace = typeof(Exception).GetMethod("InternalPreserveStackTrace", PrivateInstance); private ExceptionDispatchInfo(Exception source) { _exception = source; _stackTrace = _exception.StackTrace + Environment.NewLine; _source = Source.GetValue(_exception); } public Exception SourceException { get { return _exception; } } public static ExceptionDispatchInfo Capture(Exception source) { if (source == null) throw new ArgumentNullException("source"); return new ExceptionDispatchInfo(source); } public void Throw() { try { throw _exception; } catch { InternalPreserveStackTrace.Invoke(_exception, new object[0]); RemoteStackTrace.SetValue(_exception, _stackTrace); Source.SetValue(_exception, _source); throw; } } }