gpt4 book ai didi

aop - Postsharp 新手 - 为什么 args.Instance 为空?

转载 作者:行者123 更新时间:2023-12-03 17:49:56 24 4
gpt4 key购买 nike

PostSharp 的新手 --- 我现在正在试用 NuGet 版本,我试图在 AuthoriseAttribute OnEntry 方法中理解 agrs.Instance 值为 null 的原因。我正在尝试实现取决于对象值的授权,例如已存档的客户不能提高信用额度。我在特定于规则的其他类中实现规则。

public class Program
{
static void Main(string[] args)
{
var c = new Customer();
c.RaiseCreditLimit(100000);
c.Error(00);
}
}

public class Customer
{
[AuthorizeActivity]
public void RaiseCreditLimit(int newValue)
{
}

[AuthorizeActivity]
public void Error(int newValue)
{

}
}

[Serializable]
public class AuthorizeActivityAttribute : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
//
//Why is args.Instance null???????????
//
if (args.Method.Name == "RaiseCreditLimit")
{
Debug.WriteLine(args.Method.Name + " started");
}
else
{
throw new Exception("Crap");
}
}

public override void OnExit(MethodExecutionArgs args)
{
Debug.WriteLine(args.Method.Name + " finished");
}
}

最佳答案

答案是因为你没有在你的方面使用它。这是一个优化。如果您在方面使用它,那么它将被设置。改变你的方面来消费实例,它就会在那里。

public override void OnEntry(MethodExecutionArgs args)
{
//
//Why is args.Instance null???????????
//
if (args.Method.Name == "RaiseCreditLimit")
{
Debug.WriteLine(args.Instance.GetType().Name);
Debug.WriteLine(args.Method.Name + " started");
}
else
{
throw new Exception("Crap");
}
}

有关更多信息,请查看本文以了解 PostSharp 在优化代码方面还做了哪些工作 http://programmersunlimited.wordpress.com/2011/03/23/postsharp-weaving-community-vs-professional-reasons-to-get-a-professional-license/

关于aop - Postsharp 新手 - 为什么 args.Instance 为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6559589/

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