Closed dzmitry-lahoda closed 9 years ago
Hi asd-and-Rizzo,
This can be done in the current version by creating a class that implements IMixinInterceptor
and provides try/catch functionality in the OnAfterMethodInvocation
method. Easiest way to do this is to inherit from MixinInterceptorBase
:
public class TryCatchInterceptor : MixinInterceptorBase
{
public override void OnAfterMethodInvocation(object sender, MethodEventArgs eventArgs)
{
if (null == eventArgs.MemberInvocationException)
//Nothing to do, method did not throw exception
return;
//Log Error
bool handleError = true;
if (handleError)
{
eventArgs.CancellationToken = new CancellationToken();
//if necessary set the value to be returned.
eventArgs.ReturnValue = new object();
}
else
{
//Do nothing. The exception will be thrown.
}
}
}
You can them use this Interceptor:
[pMixin(Target = typeof(WhateverYouWant), Interceptors = new[] { typeof(TryCatchInterceptor)})]
public partial class Example{}
Note: This support will likely be deprecated in the future. Other tools (such as PostSharp) provide this functionality in a more robust and universal manner. Keeping support for Interceptors additionally complicates the pMixins code base and doesn't directly contribute to it's core goal of Composition.
Thanks for clarification of core goal and sample. But several points I may add: 1."pMixins Code Generation Framework" implies support for case like I mentioned. I suggest to rename "pMixins Composition Code Generation Add-in for Visual Studio" if not.
Framework is on what you can build several applications/libraries/tools. .NET and ASP.NET vNEXT are frameworks. pMinixs not. What may be expected from code generation Framework?
On first layer NRefactory or Roslyn are added with simplest shortcuts to do things with code.
Second layer is object oriented procedural code to do code generation/manipulation and VALIDATION.
Third layer is view on this code: mocking library style DSL or composition oriented attribute based library.
Forth is application/tools: out of T4 or out of VS Add-in.
class B
{
int Foo(int a,int b)
{
try
{
return a/b;//embed body direcly
}
catch(Exceptions a)
// wrapper actions
}
}
Update docs to show how to do next if possible.
Given
Generate
Wrapper actions
Defined how to work if A and B has same interface. Define if we have A1,A2,A3 to generated several B(is why at all need such generation). Defined some query to filter out some methods which should not be wrapped.