您可以从 PostSharp 建议中呼叫班级成员吗?
因此,我正在与 PostSharp 合作提取样板日志记录/异常处理代码,以便:
public void doSomething()
{
Logger.Write("Entered doSomething");
try
{
// code
}
catch (Exception ex)
{
ExceptionPolicy.HandleException(ex, "Errors");
}
Logger.Write("Exited doSomething");
}
变成这样:
[Log]
[HandleExceptions]
public void doSomething()
{
// code
}
但是,在某些地方,我的代码有一个存在已知恢复点的实例,所以它看起来像:
public void doSomethig()
{
try
{
// code
}
catch (KnownException ex)
{
ExceptionPolicy.HandleException(ex, "Known");
}
finally
{
this.Recover();
}
}
我想将此表示为建议,但我似乎无法从建议中访问班级成员。
So I'm working with PostSharp to pull out boilerplate logging/exception handling code so that this:
public void doSomething()
{
Logger.Write("Entered doSomething");
try
{
// code
}
catch (Exception ex)
{
ExceptionPolicy.HandleException(ex, "Errors");
}
Logger.Write("Exited doSomething");
}
becomes this:
[Log]
[HandleExceptions]
public void doSomething()
{
// code
}
but, in certain places I have code that has an instance where known recovery points exist, so it looks like:
public void doSomethig()
{
try
{
// code
}
catch (KnownException ex)
{
ExceptionPolicy.HandleException(ex, "Known");
}
finally
{
this.Recover();
}
}
I'd like to represent this as an advice but I can't seem to get access to members of the class from the advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。要从切面调用目标类的成员,您必须将此成员导入到切面中。请参阅http ://doc.sharpcrafters.com/postsharp/2.0/Content.aspx/PostSharp.chm/html/e2086a16-ba9e-43b6-b322-12021b6f24c8.htm。
Yes. To invoke a member of the target class from an aspect, you have to import this member into the aspect. See http://doc.sharpcrafters.com/postsharp/2.0/Content.aspx/PostSharp.chm/html/e2086a16-ba9e-43b6-b322-12021b6f24c8.htm.