gpt4 book ai didi

c# - 如何使用 PostSharp 停止方法的执行?

转载 作者:行者123 更新时间:2023-12-02 21:28:18 28 4
gpt4 key购买 nike

目前我正在尝试开发一个解决方案,该解决方案将检查一个方法是否已被执行,以及自上次执行以来是否已经过了一段时间,鉴于它是并且时间已经过去,我想从 OnEntry 跳过方法到 OnExit,而不实际执行该方法本身的任何代码。

排序:

public class CacheThisMethod : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
if (isCached( args.Method.name)
{
args.MethodExecutionTag = getReturnValue(args.Method.name)
//jump to OnExit
}
else
{
//continue
}
}
public override void OnExit(MethodExecutionArgs args)
{
args.Method.ReturnValue = args.MethodExecutionTag;


}
}

我怎样才能实现这个目标?谢谢。

最佳答案

以下对代码的修改显示了如何获得您想要的内容。

public class CacheThisMethod : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
if (isCached( args.Method.name)
{
args.MethodExecutionTag = getReturnValue(args.Method.name)
OnExit(args);
}
else
{
//continue
}
}
public override void OnExit(MethodExecutionArgs args)
{
//args.Method.ReturnValue = args.MethodExecutionTag;
args.ReturnValue = args.MethodExecutionTag;
args.FlowBehavior = FlowBehavior.Return;
}
}

但是,如果您正在处理每个方法名称键,那么您可以使用一个简单的属性作为缓存的返回值,因为当您将建议附加到方法时,将为您创建每个方面的单独实例。

如果没有理由跳转到 OnExit,则只需在调用 OnExit 方法时将 FlowBehaviour 和返回值设置添加到 OnEntry 方法即可。

关于c# - 如何使用 PostSharp 停止方法的执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22969452/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com