このコードの使用
[TestMethod]
public void Should_Wrap_Exception_ThrownByTarget()
{
var container = new UnityContainer();
container.RegisterType<Target>(
new Interceptor<VirtualMethodInterceptor>(),
new InterceptionBehavior<PolicyInjectionBehavior>());
container.AddNewExtension<Interception>();
var config = container.Configure<Interception>();
config.AddPolicy("1").AddCallHandler<ExceptionHandler>().AddMatchingRule<AlwaysMatches>();
var target = container.Resolve<Target>();
target.AlwaysThrows("foo");
}
public class AlwaysMatches : IMatchingRule
{
public bool Matches(MethodBase member)
{
return true;
}
}
public class ExceptionHandler : ICallHandler
{
public int Order { get; set; }
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
IMethodReturn r = getNext()(input, getNext);
if (r.Exception != null)
{
throw new InvalidOperationException("CallHandler", r.Exception);
}
return r;
}
}
public class Target
{
public virtual string AlwaysThrows(string foo)
{
throw new Exception("Boom!");
}
}
このようなスタックトレースを取得します
at UnityExceptionAspect.Target.AlwaysThrows(String foo) in C:\VisualStudio\Evaluation\UnityExceptionAspect\Target.cs:line 9
at DynamicModule.ns.Wrapped_Target_c49f840ef38c41d7b4d5956223e95f73.<AlwaysThrows_DelegateImplementation>__0(IMethodInvocation inputs, GetNextInterceptionBehaviorDelegate getNext)
整形が下手ですみません…
生成された型に関する暗号化された情報によって難読化されていますが、例外の元のソースが確実に含まれています。