开发者

How do i print any value after Main() method gets called?

开发者 https://www.devze.com 2023-03-26 16:27 出处:网络
I have the following code where I\'m printing values before the Main() method gets called by using a static constructor. How can I print another value after Main() has returned, without modifying the

I have the following code where I'm printing values before the Main() method gets called by using a static constructor. How can I print another value after Main() has returned, without modifying the Main() method?

I want output like:

1st 
2nd 
3rd

The "base" code I use:

class Myclass
{        
    static void Main(string[] args)
    {
        Console.WriteLine("2nd");
    }              
}  

I added a static constructor to Myclass for displaying "1st"

class Myclass
{     
static Myclass() 开发者_高级运维{ Console.WriteLine("1st"); }   //it will print 1st 
    static void Main(string[] args)
    {
        Console.WriteLine("2nd"); // it will print 2nd
    }              
}

Now What i need to do to is to print 3rd without modifying the Main() method. How do I do that, if it is at all possible?


Continuing on your same thought with the Static constructor, you can use the AppDomain.ProcessExit Event leaving Main() untouched.

class Myclass
    {
        // will print 1st also sets up Event Handler
        static Myclass()
        {
            Console.WriteLine("1st");
            AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
        }

        static void Main(string[] args)
        {
            Console.WriteLine("2nd"); // it will print 2nd
        }

        static void CurrentDomain_ProcessExit(object sender, EventArgs e)
        {
            Console.WriteLine("3rd");
        }
    }


There are a couple of events you can attach to, to catch the Application's Exit events:

.NET Console Application Exit Event

But I am wondering what you are trying to achieve here? Are you sure you can't change your Main method? If not, why?

Can't you separate out the method body of Main into another method, and make your Main look like this:

class Myclass
{     
static Myclass() 
    static void Main(string[] args)
    {
        Console.WriteLine("1st");
        Process(args);
        Console.WriteLine("3rd");
    }  

    static void Process(string[] args) {
        Console.WriteLine("2nd"); // it will print 2nd
    }
}


You can't in C#. In other languages such as C++ it is possible to do this in a destructor of a static or global object, but finalizers in C# are non-deterministic. They may not even be called at all if the object isn't garbage collected before the process is ended.


Add another class with a static Main suitable as a program entry point. In this call Myclass.Main:

class MyOtherClass {
  static void Main(string[] args) {
    Console.WriteLine("1st");
    Myclass.Main(args);
    Console.WriteLine("3rd");
  }
}

and then change the build options to select MyOtherClass as the program entry point. In VS this is done in Project Properties | Application | Start up object. With the command line use the /main:typename option to `csc.exe.

0

精彩评论

暂无评论...
验证码 换一张
取 消