locked
ClassCleanUp in Base Class RRS feed

  • Question

  • Hi, All,

    I would like to Use [ClassInitialize] and [ClassCleanUp] attributes in a base class, but I found that the method with [ClassInitialize] and [ClassCleanUp] attributes in base class never be executed.

    How can I make it able to be executed in base class?

    Many thanks.

    Saturday, April 28, 2012 2:45 AM

Answers

  • Hello,

    It's by design but fortunately there is a way to make it work.

    [TestClass]
    public class TestBase()
    {
    	// ClassInitialize
    	static TestBase()
    	{
    		// Write your initialisation code here.
    	
    		 AppDomain.CurrentDomain.DomainUnload += CurrentDomain_DomainUnload
    	}
    
    	// ClassCleanUp
    	static CurrentDomain_DomainUnload(object sender, EventArgs e)
    	{
    		// Write your cleanup code here.
    	}
    }


    Eyal (http://shilony.net), Regards.


    • Edited by Eyal Solnik Sunday, April 29, 2012 7:35 AM
    • Marked as answer by Damon Wang Wednesday, May 2, 2012 1:04 AM
    Sunday, April 29, 2012 7:34 AM

All replies

  • Manually:

    [ClassInitialize()]
    public static void ClassInit()
    {
        base.YourMethod();
    }


    • Edited by cicatrixx Saturday, April 28, 2012 6:37 AM
    Saturday, April 28, 2012 6:37 AM
  • I have tried it, but it doesn't work. That is why I come here to ask for help. When I run TestMethod from 'this' class, it never be executed. However if I defined this method into 'this' class, it can be executed successfully.
    Saturday, April 28, 2012 6:42 AM
  • Use a static constructor in the base class. 
    Sunday, April 29, 2012 6:19 AM
  • Hello,

    It's by design but fortunately there is a way to make it work.

    [TestClass]
    public class TestBase()
    {
    	// ClassInitialize
    	static TestBase()
    	{
    		// Write your initialisation code here.
    	
    		 AppDomain.CurrentDomain.DomainUnload += CurrentDomain_DomainUnload
    	}
    
    	// ClassCleanUp
    	static CurrentDomain_DomainUnload(object sender, EventArgs e)
    	{
    		// Write your cleanup code here.
    	}
    }


    Eyal (http://shilony.net), Regards.


    • Edited by Eyal Solnik Sunday, April 29, 2012 7:35 AM
    • Marked as answer by Damon Wang Wednesday, May 2, 2012 1:04 AM
    Sunday, April 29, 2012 7:34 AM
  • Thank you. It should be worked. I found another way is that use ~() method to execute clean up method, it also can work.
    Wednesday, May 2, 2012 1:05 AM