【发布时间】:2016-04-18 09:54:28
【问题描述】:
之后: Custom Attribute not being hit 我正在寻找在我的应用程序中点击方法属性的方法。
目前我知道是否有一些自定义属性,例如
[AttributeUsage(AttributeTargets.All)]
public class MyAttribute : Attribute
{
public MyAttribute()
{
Console.WriteLine("Hello");
}
}
以及用 MyAtrtribute 装饰的方法,例如:
[MyValidation]
public class Service
{
[MyValidation]
public bool GetService(int id)
{
if (id > 100)
{
return true;
}
return false;
}
}
触发该属性的方法是调用方法service.GetType().GetCustomAttributes(false)。 问题是:如果我想在许多类中装饰许多方法,应用属性的方式是什么?就我而言,我想在获取 createProject()、createUser() 等方法调用时应用许可证验证。
像service.GetType().GetCustomAttributes(false)这样的编码方式似乎太麻烦了。
我想要一些中心位置来负责触发应用的属性。有一些优雅的解决方案吗?
谢谢!
【问题讨论】:
标签: c# validation attributes custom-attributes