Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 399867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:53:18+00:00 2026-05-12T16:53:18+00:00

Im fully aware of the problem with static event handlers from a GC perspective

  • 0

Im fully aware of the “problem” with static event handlers from a GC perspective so i’m not looking for advice on “DONT use static events” or anything like that, in my scenario this isnt a concern.

I have a static class which has an event declared

public static event EventHandler<MyEventArgs> FilePickedUpFromStorage;

i have a client service that subscribes to this event and im wanting to Mock/test the static event being fired with a fake MyEventArgs to assert the handling works as specified, at the client. Straightforwards stuff…. the problem i have is that this event is static on a static class. im looking for some solid guidance on the best approach to dealing with this, if anyone can offer any help. Changing the static event is not an option, wrapping it or any other magic is…

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:53:18+00:00Added an answer on May 12, 2026 at 4:53 pm

    You could keep the static event for “legacy compatibility” and provide a better structure for new (and testable) code.

    // legacy
    public static class OldClass
    {
      public static event EventHandler<MyEventArgs> FilePickedUpFromStorage;
    }
    
    // new interface for testability
    public interface IBraveNewWorld
    {
      event EventHandler<MyEventArgs> FilePickedUpFromStorage;
    }
    
    // new implementation
    public class BraveNewWorld : IBraveNewWorld
    {
      public event EventHandler<MyEventArgs> FilePickedUpFromStorage;
      public BraveNewWorld()
      {
        // MyHandler forwards the event
        OldClass.FilePickedUpFromStorage += MyHandler;
      }
    }
    
    // new testable user of the event.
    public class TestableClass
    {
      // here you can pass a mock or just an instance of BraveNewWorld
      public TestableClass(IBraveNewWorld x)
      {
    
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Being resonably new to using GTK+, im not fully aware of all its functionality.
I'm not fully getting all i want from Google analytics so I'm making my
We have a problem with deserializing an exception. I am fully aware that it's
I'm fully aware of the impact of flagging a thread as a daemon thread
i do not fully understand how to communicate between adobe air (using flex3) and
Please not that I fully understand this is a dumb ass idea, but its
Not sure I fully understand what phpmyadmin does. I created a database in phpmyadmin,
I recently ran into a quite complex problem and after looking around a lot
First, I am fully aware about PEP8 , but sometimes it isn't very clear
Disclaimer Yes, I am fully aware that what I am asking about is totally

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.