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(); } 
4
  • 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. 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). Commented Jul 11, 2013 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. Commented Jul 11, 2013 at 14:56
  • @dasblinkenlight - The constructor of the attribute does not get called in the code listed in the question. See my answer below. Commented Jul 11, 2013 at 15:03
  • possible duplicate of How do attribute classes work? Commented Jul 11, 2013 at 16:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

"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
Thanks, I took out the part about calling base class constructors :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.