0

I'm working on an auditing piece and am trying to use an attribute to mark parameters to a method that should be recorded in the audit for additional information. However, for whatever reason, I can't seem to check for whether the attribute exists.

My code:

  [Audit(AuditType.GetReport)]
  public Stream GetReportStream([AuditParameter] Report report)
  {
     ...
  }

  [AttributeUsage(AttributeTargets.Parameter)]
  public class AuditParameterAttribute : Attribute
  {
  }

And, inside the interceptor where I'm trying to get it:

foreach (ParameterInfo param in invocation.Method.GetParameters ())
{
   var atts = CustomAttributeData.GetCustomAttributes (param);
   if (param.IsDefined (typeof(AuditParameterAttribute), false))
   {
      attributes.Add (param.Name, invocation.Arguments[param.Position].ToString ());
   }
}

I started adding on some extra calls to try to get something to work; what's why the extra var atts is in there. The invocation variable has information about the method that's called, and I am able to get a ParameterInfo object representing the parameter out of it. But, no matter what I've tried, I never can get any custom attributes out of it.

What am I doing wrong here?

fire.eagle
  • 2,723
  • 1
  • 21
  • 23

1 Answers1

2

Got it. Turns out it was a problem with my inexperience with using Castle. I realized that it was going through a proxy based on the interface for the invoked class, which didn't have the attribute I was looking for. So, changing my code to this:

foreach (ParameterInfo param in invocation.MethodInvocationTarget.GetParameters ())
{
   if (param.IsDefined (typeof(AuditParameterAttribute), false))
   {
      attributes.Add (param.Name, invocation.Arguments[param.Position].ToString ());
   }
}

Using MethodInvocationTarget instead of Method fixed the problem.

fire.eagle
  • 2,723
  • 1
  • 21
  • 23