Why does my .NET Attribute not perform an action?

后端 未结 3 1051
遇见更好的自我
遇见更好的自我 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:07

    Attributes only allow decoration of types and members, but the attribute itself cannot acces the decorated object. You will have to use the constructor parameters of the attribute to pass in any data you require to work with within the attribute.

    If you wish to use attributes to automatically alter the behaviour of their target objects, you will have to look at AOP solutions like PostSharp.

提交回复
热议问题