2017-03-29 109 views
1

我有MethodInterceptionAspect(PostSharp)的实现 但是当我在覆盖OnInvoke法,args.Method为null, 我需要知道方法的返回值类型,Postsharp MethodInterceptionAspect获得方法的返回值

任何人都知道?

[PSerializable] 
public class PSHandleRequestAttribute : MethodInterceptionAspect 
{ 
    public PSHandleRequestAttribute(bool readOnly = true) : base() 
    { 
     ReadOnly = readOnly; 
    } 

    #region Properties 

    protected bool ReadOnly { get; set; } 

    #endregion Properties 

    #region Public Methods 

    public override void OnInvoke(MethodInterceptionArgs args) 
    { 
     var instance = args.Instance as IBusinessTransaction; 
     var method = args.Method; 
     if (instance.IsNull()) 
     { 
      throw new Exception("Use PSHandleRequestAttribute only for IBusinessTransaction"); 
     } 

     instance.OpenTransaction(); 

     try 
     { 
      args.Proceed(); 
      //base.OnInvoke(args); 
      instance.CommitTransaction(); 
      return; 
     } 
     catch (Exception ex) 
     { 
      var errorMessage = instance.RollbackTransaction(ex); 

      return; 
     } 
    } 

    #endregion Public Methods 
} 

回答

1

PostSharp优化产生的代码,所以当的args.Method的值没有被任何地方使用,优化器跳过某些操作和传递空作为值。

在代码中使用该值后,该值应该出现。

我还建议您在CompileTimeValidate方法中进行使用验证并发出编译时错误消息。这样你就可以捕捉到构建时可能出现的错误。请参阅http://doc.postsharp.net/aspect-validation

+0

感谢!,它的工作!,还可以在'CompileTimeValidate'中保存返回值类型,并在运行时使用 – Shoshana