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 小时前
【flutter】TextField输入框工具栏文本为英文解决(不用安装插件版本
前端·javascript·flutter
*.✧屠苏隐遥(ノ◕ヮ◕)ノ*.✧1 小时前
C语言_数据结构总结8:链式队列
c语言·开发语言·数据结构·链表·visualstudio·visual studio
—Qeyser2 小时前
用Deepseek写一个 HTML 和 JavaScript 实现一个简单的飞机游戏
javascript·游戏·html
千里码aicood2 小时前
[含文档+PPT+源码等]精品基于Python实现的校园小助手小程序的设计与实现
开发语言·前端·python
讨厌下雨的天空2 小时前
C++之list
开发语言·c++·list
青红光硫化黑2 小时前
React基础之React.memo
前端·javascript·react.js
大麦大麦2 小时前
深入剖析 Sass:从基础到进阶的 CSS 预处理器应用指南
开发语言·前端·css·面试·rust·uni-app·sass
hhw1991122 小时前
c#面试题整理6
java·开发语言·c#
蠟筆小新工程師2 小时前
Deepseek可以通过多种方式帮助CAD加速工作
开发语言·python·seepdeek
GDAL3 小时前
better-sqlite3之exec方法
javascript·sqlite