Why does my .NET Attribute not perform an action?

后端 未结 3 1081
遇见更好的自我
遇见更好的自我 2021-01-06 00:27

I\'ve created a simple Attribute:

[AttributeUsage(AttributeTargets.Method)]
public class InitAttribute : System.Attribute
{
    public InitAttribute()
    {
         


        
3条回答
  •  青春惊慌失措
    2021-01-06 01:05

    The attribute is never actually instantiated and so its constructor is never called. The attribute remains as meta-data until you use reflection to retrieve it. As has been mentioned previously what you are after is an Aspect Oriented Programming tool. PostSharp works by altering the assembly as a post-build step. If you are using the Castle Windsor or Unity Inversion of Control Containers they both offer AOP capabilities as well.

提交回复
热议问题