有没有类似的ExceptionDispatchInfo http://msdn.microsoft.com/en-us/library/system.runtime.exceptionservices.exceptiondispatchinfo%28v=vs.110%29.aspx in 微软Bcl.Async https://www.nuget.org/packages/Microsoft.Bcl.Async?我找不到类似的东西。
这个问题是由另一个问题 https://stackoverflow.com/q/20170527/2674222我的。当异常的父级task
可用,我可以使用task.GetAwaiter().GetResult()
按照@StephenCleary 的建议重新抛出。
当它不可用时我有什么选择?
这是执行ExceptionDispatchInfo
来自莫诺 https://github.com/mono/mono/blob/master/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs。据我测试,它似乎与 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;
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)