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?