Closed kargencW closed 4 years ago
Hi, for this you want to unwrap InvokationExceptions, like this:
[Advice(Kind.Around)]
public object Handle(
[Argument(Source.Target)] Func<object[], object> target,
[Argument(Source.Arguments)] object[] args,
[Argument(Source.Name)] string name,
[Argument(Source.ReturnType)] Type retType
)
{
var operationName = $"{target.Method.DeclaringType.FullName}:{name}";
if (typeof(Task).IsAssignableFrom(retType))
{
var syncResultType = retType.IsConstructedGenericType ? retType.GenericTypeArguments[0] : _voidTaskResult;
var tgt = target;
return InvokeWrapper(_asyncHandler.MakeGenericMethod(syncResultType), target, args, operationName);
}
else
{
retType = retType == typeof(void) ? typeof(object) : retType;
return InvokeWrapper(_syncHandler.MakeGenericMethod(retType), target, args, operationName);
}
}
private object InvokeWrapper(MethodInfo method, Func<object[], object> target, object[] args, string opname)
{
try
{
return method.Invoke(this, new object[] { target, args, opname });
}
catch (TargetInvocationException te)
{
throw te.InnerException;
}
}
class Program
{
static void Main(string[] args)
{
try
{
try
{
throw new customexception();
}
catch (customexception ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
catch (Exception ex)
{
throw ex;
}
}
}
class customexception : ApplicationException
{
}
this also cannot work, why inner customexception not working, any idea ?
Because they get wrapped by method.Invoke(). See my comment above
catch (TargetInvocationException te) { throw te.InnerException; }
solve my problem, thank you very much
this is my aspect class
and this is test code
i never catch customexception in main try catch block, how can i solve this case?