在Microsoft.Bcl.Async中是否有ExceptionDispatchInfo
的模擬?我找不到任何類似的東西。Microsoft.Bcl.Async中是否有ExceptionDispatchInfo的模擬?
這個問題是由我的another question觸發的。當例外的父母task
可用時,我可以使用task.GetAwaiter().GetResult()
重新拋出,正如@StephenCleary建議的那樣。
什麼是我的選擇何時不可用?
在Microsoft.Bcl.Async中是否有ExceptionDispatchInfo
的模擬?我找不到任何類似的東西。Microsoft.Bcl.Async中是否有ExceptionDispatchInfo的模擬?
這個問題是由我的another question觸發的。當例外的父母task
可用時,我可以使用task.GetAwaiter().GetResult()
重新拋出,正如@StephenCleary建議的那樣。
什麼是我的選擇何時不可用?
我已經試驗了implementation provided by avo - 但它沒有通過所有的測試。我的意思是它提供了基本的行爲,但在更復雜的情況下會失敗(如多次投擲,重新奪回等)。
作爲Theraot.Core的一部分,我以這樣的方式創建了這個類的後端端口,它可以在現代的Mono,舊的Mono(2.6 *之前的版本)以及2.0到4.0的任何Microsoft .NET中工作。
*:隨着Miguel de Icaza的:)稍尖
下面的代碼目前只在特性分支,它最終將移動到主分支(與任務沿着.NET 2.0 :),此時它將通過Theraot.Core nuget提供。如果有人需要它,我會把它留在這裏。
如果您發現任何問題,歡迎在項目的github(鏈接上方)的問題跟蹤器上發現錯誤報告。
#if NET20 || NET30 || NET35 || NET40
using System.Reflection;
namespace System.Runtime.ExceptionServices
{
/// <summary>
/// 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.
/// </summary>
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;
}
}
/// <summary>
/// Creates an ExceptionDispatchInfo object that represents the specified exception at the current point in code.
/// </summary>
/// <param name="source">The exception whose state is captured, and which is represented by the returned object.</param>
/// <returns>An object that represents the specified exception at the current point in code. </returns>
public static ExceptionDispatchInfo Capture(Exception source)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
return new ExceptionDispatchInfo(source);
}
/// <summary>
/// Gets the exception that is represented by the current instance.
/// </summary>
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);
}
/// <summary>
/// Throws the exception that is represented by the current ExceptionDispatchInfo object, after restoring the state that was saved when the exception was captured.
/// </summary>
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
下面是執行ExceptionDispatchInfo
from Mono。據我所測試,它似乎與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;
}
}
}