PostSharp: OnMethodBoundaryAspect on derived class's constructor
I have an aspect that writes something to the console on exception. I have a base class that throws an exception on its constructor, and a derived class that have the aspect on its constructor.
I would expect that the derived class aspect on constructor will catch the base class exception, but it dont.
Is this by design? Is this a bug? Or am I doing something wrong?
Here is sample code (Console Application):
[Serializable]
public class OnExceptionWriteAspect : OnMethodBoundaryAspect
{
public override void OnException(MethodExecutionEventArgs eventArgs)
{
Console.WriteLine("Exception catched on Aspect.");
}
}
public class BaseClass
{
public BaseClass()
{
throw new Exception();
}
}
public class DerivedClass : BaseClass
{
[OnExceptionWriteAspect]
public DerivedClass()
: base()
{
}
}
public class Program
{
static v开发者_StackOverflowoid Main(string[] args)
{
try
{
new DerivedClass();
}
catch
{
Console.WriteLine("Exception catched on Main.");
}
}
}
The output is:
Exception catched on Main.
This is by design. There is no way to put an exception handler around the call to the base constructor. The MSIL code would not be verifiable.
If you look at the DerivedClass
via reflector you see that the Aspect only wraps the DerivedClass
's constructor.
public class DerivedClass : BaseClass
{
// Methods
public DerivedClass()
{
MethodExecutionEventArgs ~laosEventArgs~1;
try
{
~laosEventArgs~1 = new MethodExecutionEventArgs(<>AspectsImplementationDetails_1.~targetMethod~1, this, null);
<>AspectsImplementationDetails_1.MyTest.OnExceptionWriteAspect~1.OnEntry(~laosEventArgs~1);
if (~laosEventArgs~1.FlowBehavior != FlowBehavior.Return)
{
<>AspectsImplementationDetails_1.MyTest.OnExceptionWriteAspect~1.OnSuccess(~laosEventArgs~1);
}
}
catch (Exception ~exception~0)
{
~laosEventArgs~1.Exception = ~exception~0;
<>AspectsImplementationDetails_1.MyTest.OnExceptionWriteAspect~1.OnException(~laosEventArgs~1);
switch (~laosEventArgs~1.FlowBehavior)
{
case FlowBehavior.Continue:
case FlowBehavior.Return:
return;
}
throw;
}
finally
{
<>AspectsImplementationDetails_1.MyTest.OnExceptionWriteAspect~1.OnExit(~laosEventArgs~1);
}
}
}
public BaseClass()
{
throw new Exception();
}
If you want to handle Aspect Inheritance look at using MulticastAttributeUsage
精彩评论