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;

}

相关推荐
武当豆豆4 分钟前
C++编程学习(第42天)
开发语言·c++·学习
Baklib梅梅18 分钟前
探码科技再获“专精特新”认定:Baklib引领AI内容管理新方向
前端·ruby on rails·前端框架·ruby
南方以南_23 分钟前
Chrome开发者工具
前端·chrome
YiHanXii30 分钟前
this 输出题
前端·javascript·1024程序员节
楊无好37 分钟前
React中ref
前端·react.js
维他命Coco37 分钟前
js常见开发学习
javascript
程琬清君39 分钟前
vue3 confirm倒计时
前端·1024程序员节
yong99901 小时前
基于MATLAB的内容图像检索实现
开发语言·matlab
歪歪1001 小时前
在C#中详细介绍一下Visual Studio中如何使用数据可视化工具
开发语言·前端·c#·visual studio code·visual studio·1024程序员节
唔661 小时前
flutter实现web端实现效果
前端·flutter