.NET属性在不同目标上的优先级

时间:2018-11-13 09:49:22

标签: c# asp.net .net attributes

我有一个控制器类,在它上面包含一个用于记录系统事件的属性。

问题是我在控制器中的某些方法包含敏感数据,我不想将其保存在日志中(例如密码)。

我尝试向属性添加一个布尔属性,如果特定方法包含敏感数据,该属性应向我发出信号,并在方法目标上调用该属性(我将相同的属性用于类目标,只是将标志打开)但似乎类目标上方的属性是具有“优先级”的属性,这意味着我的敏感数据标志始终为false。

有人在使用上面的特定方法时知道如何使我的方法目标覆盖类目标吗?

在此处添加代码示例:

public class SystemEventAtt : Attribute {

public bool ContainsSensitiveData{ get; set; }

***
some additional code which logs stuff..
***

}

[SystemEventAtt]
public class SomeController : Controller {


[Route("someRoute")]
[SystemEventAtt(ContainsSensitiveData = true)]
[HttpPost]

public SomeObj FuncWhichContainsSensitiveData([FromBody] SomeOtherObj obj){

..some code
}

}

1 个答案:

答案 0 :(得分:0)

  

有人在使用上面的特定方法时知道如何使我的方法目标覆盖类目标吗?

如果您只想使用SystemEventAtt属性,我认为答案是否定的。

为什么不只是将use属性添加到期望的写日志操作中?

或者还有另一种方式。

您可以创建一个属性来表示要写入日志,并且可以在WriteLog属性中使用反射来获取是否标记bool属性的操作,而不是使用public class WriteLogAttribute : Attribute { } public class SystemEventAtt : Attribute { *** some additional code which logs stuff.. using reflection to get WriteLogAttribute check whether write log *** }

username