【发布时间】:2010-11-10 15:42:17
【问题描述】:
有没有办法编写一个扩展 .NET Attribute 类的自定义类,以便我可以将该类应用于方法,并且 .NET Framework 在执行该方法之前调用我的类构造函数。这是一个例子
public class TestAttribute : Attribute
{
public TestAttribute()
{
Console.WriteLine("Called");
}
}
class Program
{
static void Main(string[] args)
{
TestMethod();
}
[TestAttribute()]
public static void TestMethod()
{
}
}
我希望在调用TestMethod时,框架会执行TestAttribute()构造函数。
【问题讨论】: