3

I have create a attribute to process some info before a method is called but it is not getting called.

I want to log some values which are process and stored in a static field in the class, as a result of other methods of my class called.

So can someone guide on it.

[AttributeUsage(AttributeTargets.Method)]
internal class MyAttrib : Attribute
{
    public MyAttrib()
    {
        //This is not getting called. what am i missing
        Console.WriteLine("My Attrib called!!");
    }
}

class MyClass
{
    public MyClass()
    {
        Console.WriteLine("Constructor Created");
    }

    [MyAttrib]
    public int Opt1()
    {
        Console.WriteLine("Op1 Performed");
        return 0;
    }

}

static void Main(string[] args)
{
        MyClass cla = new MyClass();
        cla.Opt1();
        cla.Opt2();
        Console.ReadLine();
}
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
Mehul Talajia
  • 159
  • 1
  • 9
  • 2
    What you are looking for is Aspect-Oriented Programming. This is a complex subject that I advise you against rolling your own. The popular AoP framework is [PostSharp](http://www.postsharp.net/). Before going that route, really think twice as AoP isn't someone to include lightly on a codebase. There are alternative to compile-time AoP with some Dependency Injection framework, but there are some caveats (virtual methods). – Simon Belanger Jul 11 '13 at 14:55
  • 1
    "what am i missing?" You are missing the point of defining attributes: they are not defined for that! The constructor does get called, it's console is not connected to the output, because the call happens when the assembly gets loaded. – Sergey Kalinichenko Jul 11 '13 at 14:56
  • @dasblinkenlight - The constructor of the attribute does not get called in the code listed in the question. See my answer below. – Steve Jul 11 '13 at 15:03
  • possible duplicate of [How do attribute classes work?](http://stackoverflow.com/questions/2676603/how-do-attribute-classes-work) – Erik Philips Jul 11 '13 at 16:10

1 Answers1

5

Attributes are not usually instantiated during run-time. You use reflection to obtain what attributes are applied to various parts of the code (types, fields, etc) and what the contents of the attributes are.

Read this page on MSDN regarding accessing attributes. Specifically, the part that states:


An attribute specification such as:

[Author("P. Ackerman", version = 1.1)]
class SampleClass

is conceptually equivalent to this:

Author anonymousAuthorObject = new Author("P. Ackerman");
anonymousAuthorObject.version = 1.1;

However, the code is not executed until SampleClass is queried for attributes. Calling GetCustomAttributes on SampleClass causes an Author object to be constructed and initialized as above.


One thing you might be able to do is have a base class from which all other classes you create derive from. In this base class's constructor, use reflection to identify any attributes or anything else about the class that you're interested in and do something with that information.

This doesn't actually address your statement about processing some info before a method is executed, though... I don't believe that is possible.

Steve
  • 6,334
  • 4
  • 39
  • 67
  • 2
    "In a derived class, if a base-class constructor is not called explicitly using the base keyword, then the default constructor, if there is one, is called implicitly." -- [Using Constructors](http://msdn.microsoft.com/en-us/library/ms173115(v=vs.80).aspx) – dav_i Jul 11 '13 at 16:05
  • 1
    Thanks, I took out the part about calling base class constructors :) – Steve Jul 11 '13 at 16:55