How to tell who referenced me?

  • Thread starter Thread starter JSBarten
  • Start date Start date
J

JSBarten

I have a class library that I'm using from both a windows form
application and a web services. Is there a way to tell if the
refrencing/executing assembly is a windows form application, web
service or other? Basically, I want to possibly display information
to a user in the windows form application via a messagebox, but not
any other time/instance.

Thanks
 
That's generally a bad idea. What about if you have a console
application referencing you? Then what do you do?

You should restructure your logic in your library to return the
information to the UI (whatever it is) that it needs to display it however
it wants.

Also, to answer your question, you can't tell what assemblies are
referencing your assembly. You can traverse the call stack and look at the
types that the methods are on that are calling you, but that's VERY hackney,
and I wouldn't recommend it.

You could just set a reference to System.Web.dll, and try and get the
current HttpContext. If that exists, you know you are in an ASP.NET
environment.

However, I think you should go with the original recommendation.

Hope this helps.
 
You should restructure your logic in your library to return the
information to the UI (whatever it is) that it needs to display it however
it wants.

One way to do this is with a callback. For example, create a function
delegate so status information or messages can be passed from the library
back to whoever invoked the library. The caller then has the responsibility
to process the status info (show a messagebox, write a log event, etc.).

Nicholas Paldino said:
That's generally a bad idea. What about if you have a console
application referencing you? Then what do you do?

You should restructure your logic in your library to return the
information to the UI (whatever it is) that it needs to display it however
it wants.

Also, to answer your question, you can't tell what assemblies are
referencing your assembly. You can traverse the call stack and look at
the types that the methods are on that are calling you, but that's VERY
hackney, and I wouldn't recommend it.

You could just set a reference to System.Web.dll, and try and get the
current HttpContext. If that exists, you know you are in an ASP.NET
environment.

However, I think you should go with the original recommendation.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

JSBarten said:
I have a class library that I'm using from both a windows form
application and a web services. Is there a way to tell if the
refrencing/executing assembly is a windows form application, web
service or other? Basically, I want to possibly display information
to a user in the windows form application via a messagebox, but not
any other time/instance.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top