Код: Выделить всё
public abstract class InterceptorProxy: DispatchProxy
{
private T? _decorated;
protected override object? Invoke(MethodInfo? targetMethod, object?[]? args)
{
try
{
OnBefore(targetMethod,args);
var result = targetMethod?.Invoke(_decorated, args);
OnAfter(targetMethod, args, result);
return result;
}
catch (TargetInvocationException e)
{
OnException(targetMethod, args, e);
throw;
}
}
public static T? Create(T decorated)
{
object? proxy = Create();
((InterceptorProxy)proxy!)?.SetParameters(decorated);
return (T)proxy!;
}
private void SetParameters(T decorated)
{
_decorated = decorated ?? throw new ArgumentNullException(nameof(decorated));
}
protected virtual void OnException(MethodInfo? methodInfo, object?[]? args, Exception exception) { }
protected virtual void OnAfter(MethodInfo? methodInfo, object?[]? args, object? result) { }
protected virtual void OnBefore(MethodInfo? methodInfo, object?[]? args) { }
}
Код: Выделить всё
public class DispachedAttribute : Attribute
{
public string Type { get; set; }
public DispatchedAttribute(string type)
{
Type = type;
}
}
в контроллере:
Код: Выделить всё
[Dispached("POST")]
public ResponseDTO Run(RequestDTO requestDTO)
{
var result = service.Get(requestDTO);
return result;
}
Код: Выделить всё
public class myclass{
[Dispached("Do")]
public string Run(string param)
{
// do something
return param;
}
}
Подробнее здесь: https://stackoverflow.com/questions/783 ... -attribute
Мобильная версия