MethodInterceptor

目录

[1 MethodInterceptor](#1 MethodInterceptor)

[1.1 SetParameters](#1.1 SetParameters)

[1.2 /// Intercept the invocation](#1.2 /// Intercept the invocation)

  1. MethodInterceptor
    1. SetParameters

private void SetParameters(T decorated, IContextProvider contextProvider, IAttributeProvider attributeProvider)

{

_decorated = decorated;

_contextProvider = contextProvider ?? throw new ArgumentNullException(nameof(contextProvider));

_attributeProvider = attributeProvider ?? throw new ArgumentNullException(nameof(attributeProvider));

}

/// <summary>

    1. /// Intercept the invocation

/// </summary>

protected override object Invoke(MethodInfo targetMethod, object[] args)

{

var methodParameterTypes = targetMethod.GetParameters().Select(p => p.ParameterType).ToArray();

var classMethodInfo = _decorated != null

? _decorated.GetType().GetMethod(targetMethod.Name, methodParameterTypes)

: targetMethod;

var invocation = new Invocation

{

Arguments = args,

GenericArguments = targetMethod.IsGenericMethod ? targetMethod.GetGenericArguments() : new Type[0],

InvocationTarget = _decorated,

Method = targetMethod,

Proxy = this,

MethodInvocationTarget = classMethodInfo,

TargetType = _decorated != null ? _decorated.GetType() : typeof(T)

};

var methodExecutingContext = new MethodExecutingContext

{

InvocationContext = _contextProvider.GetContext(),

MethodInfo = targetMethod,

Invocation = invocation

};

var attributes = GetInvocationMethodFilterAttributes(invocation, methodExecutingContext.InvocationContext);

if (attributes.Any(a => a is NoInterceptAttribute))

{

invocation.Proceed();

return invocation.ReturnValue;

}

var methodFilterAttributes = attributes.OfType<MethodFilterAttribute>().OrderBy(x => x.Order).ToList();

var exceptionFilterAttributes = attributes.OfType<ExceptionFilterAttribute>().ToList();

var isAsync = typeof (Task).IsAssignableFrom(invocation.Method.ReturnType);

if (isAsync)

{

if (invocation.Method.ReturnType.IsGenericType && invocation.Method.ReturnType.GetGenericTypeDefinition() == typeof (Task<>))

{

var taskResultType = invocation.Method.ReturnType.GetGenericArguments()[0];

var mInfo = HandleAsyncWithTypeMethod.MakeGenericMethod(taskResultType);

if (_decorated != null)

{

methodFilterAttributes.Add(new InvocationAttribute(invocation, taskResultType));

}

invocation.ReturnValue = mInfo.Invoke(this, new object[] { methodFilterAttributes, exceptionFilterAttributes, methodExecutingContext });

}

else

{

if (_decorated != null)

{

methodFilterAttributes.Add(new InvocationAttribute(invocation));

}

invocation.ReturnValue = HandleAsync(methodFilterAttributes, exceptionFilterAttributes, methodExecutingContext);

}

}

else

{

if (_decorated != null)

{

methodFilterAttributes.Add(new InvocationAttribute(invocation));

}

HandleSync(methodFilterAttributes, exceptionFilterAttributes, methodExecutingContext);

}

return invocation.ReturnValue;

}

相关推荐
默默无闻的白夜几秒前
【Vue】初学Vue(setup函数,数据响应式, 脚手架 )
前端·javascript·vue.js
萌萌哒草头将军6 分钟前
⚡⚡⚡Rstack 家族即将迎来新成员 Rstest🚀🚀🚀
前端·javascript·vue.js
1candobetter6 分钟前
JAVA后端开发——多租户
java·开发语言
江城开朗的豌豆14 分钟前
Proxy:JavaScript中的'变形金刚',让你的对象为所欲为!
前端·javascript·面试
江城开朗的豌豆23 分钟前
JavaScript中的instanceof:你的代码真的认识'自家孩子'吗?
前端·javascript·面试
JinSo25 分钟前
create-easy-editor —— 快速搭建你的可视化编辑器
前端·前端框架·github
freyazzr27 分钟前
C++八股 | Day3 | 智能指针 / 内存管理 / 内存分区 / 内存对齐
开发语言·c++
coding随想29 分钟前
深入浅出JavaScript中的ArrayBuffer:二进制数据的“瑞士军刀”
javascript
序属秋秋秋33 分钟前
《C++初阶之入门基础》【普通引用 + 常量引用 + 内联函数 + nullptr】
开发语言·c++·笔记
Watermelo61734 分钟前
【前端实战】如何让用户回到上次阅读的位置?
前端·javascript·性能优化·数据分析·哈希算法·哈希·用户体验