You must have a client code to tell your code what the context is, and then work with it. At best, you can guess based on external factors.
If you have to guess, this is what I would be looking for:
- For ASP.NET, I would look for HttpContext.Current
- For Windows Forms, I see if there are all elements in the static OpenForms collection of the Application class.
- Windows Presentation Foundation , Application .
- , , ..
- , , , .