0
我正在審覈一塊,並試圖使用一個屬性來標記參數應該記錄在審計中的方法,以獲取更多信息。但是,無論出於何種原因,我似乎無法檢查屬性是否存在。參數屬性沒有顯示
我的代碼:
[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對象。但是,無論我嘗試過什麼,我都無法獲得任何自定義屬性。
我在這裏做錯了什麼?