特别说明:如果 Issue 报告为问题且开发成员回复确认问题之后但三天内都不能得到反馈,则视为无效Issue。
2.0.3
在使用AspectDispatchProxyp实现AOP功能时,AOP拦截的Invok方法MethodInfo中获取不到CustomAttribute。
这样使用Attribute标记方法就没有作用了。
在AOP功能中,只需要拦截其中一些方法时,一般都是使用Attribute来标记方法。
现在在代理类里,拦截方法时,Invoke方法的MethodInfo参数获取不到CustomAttribute, 就达不到预期要求了。
AOP拦截的Invok方法MethodInfo中获取不到CustomAttribute
AOP拦截的Invok方法MethodInfo中获取可以获取到CustomAttribute,获得标记在方法上的Attribute。
我是按文档的示例来的,,代码如下
属性
[AttributeUsage(AttributeTargets.Method)] public class LogAspectAttribute : Attribute { }
服务方法接口
`
public interface ITestService
{
string SayHello(string word);
}
服务类
[Injection(Proxy = typeof(LogDispatchProxy))]
public class TestService: ITestService, ITransient
{
[LogAspect]
public string SayHello(string word)
{
return $"Hello {word}";
}
}
LogDispatchProxy类的Invok方法 在该方法里,获取不到TestService.SayHello上贴的属性 LogAspectAttribute
///
/// 拦截方法
///
///
///
///
public override object Invoke(MethodInfo method, object[] args)
{
// 判断方法是否贴了指定属性
var hasAttribute = method.IsDefined(typeof(LogAspectAttribute));
// 获取指定属性
var logAspectAttribute = method.GetCustomAttribute();
Console.WriteLine("SayHello 方法被调用了");
var result = method.Invoke(Target, args);
Console.WriteLine("SayHello 方法返回值:" + result);
return result;
}
`
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
不知道是哪里写的有问题
登录 后才可以发表评论