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;

}

相关推荐
你的电影很有趣1 分钟前
lesson30:Python迭代三剑客:可迭代对象、迭代器与生成器深度解析
开发语言·python
小杨同学yx1 小时前
前端三剑客之Css---day3
前端·css
程序员编程指南2 小时前
Qt 嵌入式界面优化技术
c语言·开发语言·c++·qt
q__y__L2 小时前
C#线程同步(二)锁
开发语言·性能优化·c#
星月心城2 小时前
Promise之什么是promise?(01)
javascript
二川bro2 小时前
第二篇:Three.js核心三要素:场景、相机、渲染器
开发语言·javascript·数码相机
云泽8082 小时前
数据结构前篇 - 深入解析数据结构之复杂度
c语言·开发语言·数据结构
Mintopia3 小时前
🧱 用三维点亮前端宇宙:构建你自己的 Three.js 组件库
前端·javascript·three.js
卷卷的小趴菜学编程3 小时前
Qt-----初识
开发语言·c++·qt·sdk·qt介绍
天天进步20153 小时前
Python游戏开发引擎设计与实现
开发语言·python·pygame