有没有办法在调用CallCommonCode()
时自动调用属性构造函数CodedMethod()
?
using System;
[AttributeUsage(AttributeTargets.Method|AttributeTargets.Struct,
AllowMultiple=false,Inherited=false)]
public class CallCommonCode : Attribute
{
public CallCommonCode() { Console.WriteLine("Common code is not called!"); }
}
public class ConcreteClass {
[CallCommonCode]
protected void CodedMethod(){
Console.WriteLine("Hey, this stuff does not work :-(");
}
public static void Main(string[] args)
{
new ConcreteClass().CodedMethod();
}
}