C# 参数属性未显示

C# 参数属性未显示,c#,custom-attributes,C#,Custom Attributes,我正在做一个审计工作,并试图使用一个属性来标记一个方法的参数,该方法应该记录在审计中以获取更多信息。然而,无论出于何种原因,我似乎无法检查该属性是否存在 我的代码: [Audit(AuditType.GetReport)] public Stream GetReportStream([AuditParameter] Report report) { ... } [AttributeUsage(AttributeTargets.Parameter)] publi

我正在做一个审计工作,并试图使用一个属性来标记一个方法的参数,该方法应该记录在审计中以获取更多信息。然而,无论出于何种原因,我似乎无法检查该属性是否存在

我的代码:

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

  [AttributeUsage(AttributeTargets.Parameter)]
  public class AuditParameterAttribute : Attribute
  {
  }
在拦截器内,我正试图获取它:

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 ());
   }
}
我开始增加一些额外的电话,试图让一些工作;为什么会有额外的
var atts
invocation
变量包含有关所调用方法的信息,我可以从中获取表示参数的ParameterInfo对象。但是,无论我尝试了什么,我都无法从中获得任何自定义属性


我做错什么了?

明白了。原来是因为我没有使用Castle的经验。我意识到它正在通过一个基于被调用类接口的代理,该类没有我要寻找的属性。因此,将我的代码更改为:

foreach (ParameterInfo param in invocation.MethodInvocationTarget.GetParameters ())
{
   if (param.IsDefined (typeof(AuditParameterAttribute), false))
   {
      attributes.Add (param.Name, invocation.Arguments[param.Position].ToString ());
   }
}
使用MethodInvocationTarget而不是Method修复了该问题