0

I have met a strange thing when I decorate my Main method by a custom Attribute like this:

class Program
{
    [Attr]
    static void Main(string[] args)
    {
        Console.WriteLine("Main end..");
        Console.Read();
    }
}

class AttrAttribute : Attribute
{
    public AttrAttribute()
    {
        Console.WriteLine("Hello world!");
    }
}

Then the Console Program does not print anything. But when I debug the program, I find both the constructor of the attribute AttrAttribute and the Main method are indeed executed. Can anyone tell me the why..

And if I comment 'Console.WriteLine("Hello world!");' in the constructor of AttrAttribute, the program print "Main end.."..

Thank you every one. (And sorry for my pool English.)

Lcng
  • 706
  • 1
  • 8
  • 17
  • I just ran the same code, and basically the `AttrAttribute` constructor is being ran before the Console even exists. My guess is the console just won't exist until `Main`, the entry point of a console app, begins execution. What are you trying to accomplish with your attribute and why is it printing to the console? – Adam Venezia Feb 22 '14 at 04:24
  • It is the hosting process that's probing for the STA/MTAThread attribute. Project + Properties, Debug tab, untick the option and it will stop. – Hans Passant Feb 22 '14 at 09:35
  • Thank you, AdamVenezia, Dexters, Hans Passant. And the link @Dexters provided is very useful. – Lcng Feb 26 '14 at 03:14

1 Answers1

1

First, the thing that is clear is that you have to understand that Attributes are created when the class' Type has GetCustomAttributes() called. With that being said, it kind of makes sense that your Console.WriteLine in the attribute's constructor is not shown -- you're program hasn't "officially" started yet because it hasn't entered Main(...).

Now, when I ran the code and tested it, I found two things.

  1. I get your issue when I run with the debugger. Console.WriteLine never writes to the console.
  2. I do not get your issue when I run without the debugger.

When I run with the debugger and the Attribute's constructor hits, I can see in the debug screen for Console that it has all of the outputs/inputs being redirected. As I step through and enter Main(), the Console is still being redirected.

Commenting out the Console.WriteLine in the Attribute's constructor causes the Console to be reset once it enters Main(). This leads me to believe that calling Console.WriteLine before your program "officially" starts causes the console to get locked in place and not re-initialized.

Now this is part is just a guess, but I suspect that Visual Studio is capturing the Console in your applications instance for whatever it needs it for before it starts your application. Accessing it before, flags it to not reset/re-initialize.

TyCobb
  • 8,909
  • 1
  • 33
  • 53
  • Thank you. I just want to make sure that 'ctor of custom attribute will only be executed, when you will access it via reflection'. And now I get it. Thank you for your explanation. – Lcng Feb 26 '14 at 03:11