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;

}

相关推荐
dhxhsgrx1 小时前
PYTHON训练营DAY25
java·开发语言·python
GISer_Jing2 小时前
前端性能指标及优化策略——从加载、渲染和交互阶段分别解读详解并以Webpack+Vue项目为例进行解读
前端·javascript·vue
不知几秋2 小时前
数字取证-内存取证(volatility)
java·linux·前端
水银嘻嘻3 小时前
08 web 自动化之 PO 设计模式详解
前端·自动化
风逸hhh3 小时前
python打卡day25@浙大疏锦行
开发语言·python
刚入门的大一新生3 小时前
C++初阶-string类的模拟实现与改进
开发语言·c++
chxii5 小时前
5java集合框架
java·开发语言
老衲有点帅5 小时前
C#多线程Thread
开发语言·c#
C++ 老炮儿的技术栈5 小时前
什么是函数重载?为什么 C 不支持函数重载,而 C++能支持函数重载?
c语言·开发语言·c++·qt·算法