-1

I have the following C# code

 class Program
{
    [My]
    static void Main(string[] args)
    {
        SomeMethod();
    }

    [My]
    static void SomeMethod()
    {
        Console.WriteLine("1111");
    }
}


public class MyAttribute: Attribute
{
    public MyAttribute()
    {
        MessageBox.Show("enter");
    }
    ~MyAttribute()
    {
        MessageBox.Show("leave");
    }

}

Attribute My applies only to method Main (constructor of MyAttribute executes on entering the Main method and destructor executes on leaving them), but not for method SomeMethod.

I need feature like this: execute some portion of code (start and kill the process) in some methods, without modifying them (I think, that the attributes may be the best decision).

Please, help

1 Answers1

0

You are not using the attributes in the correct way:

Attributes should indicate a property of a class or a class member and in most of the cases are used by the consumer of the object.

Very often attributes are used with reflection to get information:

System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t);
Georgi Bilyukov
  • 645
  • 4
  • 11