[英]C# Attributes that figures out if another attribute is applied
我正在研究一個Asp.net MVC項目,我想知道是否存在一種使屬性與其他屬性對話的方法。
例如,我具有以下屬性
public class SuperLoggerAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
//Do something super
}
}
public class NormalLoggerAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
//Do something normal ONLY if the super attribute isn't applied
}
}
我有以下控制器
[NormalLogger]
public class NormalBaseController : Controller
{
}
public class PersonController: NormalBaseController
{
}
[SuperLogger]
public class SuperController: NormalBaseControler
{
}
因此,基本上,我希望我的SuperController使用SuperLogger並忽略NormalLogger(已在基礎中應用),而PersonController應該使用NormalLogger,因為它不會被SuperLogger“覆蓋”。 有沒有辦法做到這一點?
謝謝,
馳
為什么不讓SuperLoggerAttribute
從NormalLoggerAttribute
繼承並覆蓋Log
方法呢?
我認為這應該工作:
public enum LoggerTypes
{
Normal,
Super
}
public class LoggerAttribute : ActionFilterAttribute
{
public LoggerAttribute() : base()
{
LoggerType = LoggerTypes.Normal;
}
public LoggerAttribute(LoggerTypes loggerType) : base()
{
LoggerType = loggerType;
}
public LoggerTypes LoggerType
{
get;
set;
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (LoggerType == LoggerTypes.Super)
{
//
}
else
{
//
}
}
用法:
[Logger(LoggerTypes.Normal)]
要么
[Logger(LoggerTypes.Super)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.