finding current namespace in runtime


How can I determine the assembly name of the running application that hosts my DLL (runtime)?

I want to use this to do the following:
Use Activator.CreateInstance(assemblyname,typename) where assemblyname is the name of the running application and typename is a predetermined name of a class of a specific type. i know the typename, it's the assemblyname that is the problem.

I could solve this by adding an entry to web.config with the current assemblyname, but i don't want it that way
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


to get the assembly name...

dim asm as system.reflection.assembly"My assembly name is " & asm.fullname)
Darth_helgeAuthor Commented:
that gives me the value of 'nothing'
mmm... try this

        ' Get our assembly.
        Dim executing_assembly As System.Reflection.Assembly = _
        ' Get our namespace.
        Dim executing_namespace As String = _

this is taken from one of our apps and it definetely works here

OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Darth_helgeAuthor Commented:

i still get nothing reference for executing_assembly

i have to point out that this is a web application, not a windows forms application
Bob LearnedCommented:
What kind of magic are you looking to perform with loading a dynamic assembly?

crashed and burned.... that code snippet was from a winForm application.

If you are dealing with web then I think I'll bail out ;)
Darth_helgeAuthor Commented:
I want my DLL to create an instance of an object in the main running web-application, and then a get a reference to this object so i can invoke methods on the object.
Bob LearnedCommented:
When you put the DLL in the bin folder on the web server, and call Assembly.LoadFrom("..\bin\DLLFile.dll"), does it work?

Darth_helgeAuthor Commented:
my DLL can lie in different applications. and i want my DLL to load a class from the application. The DLL is to be used in several applications

the name of the class i want to invoke is predetermined, but the name of the main app may vary.
Bob LearnedCommented:
Can you add a reference to the DLL in the web application?  Would you want to put the DLL in the Global Assembly Cache (GAC) on the web server?

Darth_helgeAuthor Commented:
I have a reference to the DLL in the web app.

I have solved my problem by doing the following:

in global.asax application_onstart for all apps that use the DLL:
application.add("RoleProvider",new MyRoleProvider())
myroleprovider implements a specific interface MyInterface.

then in the DLL:
dim o as MyInterface
o = ctype(httpcontext.current.application("RoleProvider"),MyInterface)

The thing is that i want to do this without registering the class in application_onstart
I hoped that i could do somehting like this:
dim a as Reflection.Assembly = Reflection.Assembly.GetEntryAssembly()
Bob LearnedCommented:
Are you developing using something like Web Matrix, or the IDE?

Darth_helgeAuthor Commented:
i use 2005
Bob LearnedCommented:
If you have a reference, then what is the reason for defining th provider in the Global.asax file?  Do you want every one accessing the application to use the same reference?

Darth_helgeAuthor Commented:
the main app has a reference to the dll, but the dll has no reference to the main app, and i want to load a class from the main app into the dll, without knowing the name of the main app at design time
Bob LearnedCommented:
May I be so bold as to ask what you are trying to accomplish here?

Darth_helgeAuthor Commented:

I'm trying to retrive some information from the application that hosts my dll.

In this particulare case I want the application to tell me wich roles is assosiated with a certian user for the current application. The same user can be valid for several apps thats way the current application have to do this jobb. Roles and access behavior is also spesific pr application.

It's the same concept as the 'provider' model microsoft uses in .net 2.0 but i don't want my developers to registere anything in the webconfig file to make thing work.
I just want them to make a class called 'MyProviderForSomthing' wich must implement then 'IMyProviderForSomething' interface and put that class in the App_Code folder.

Maybe I'm far off... ?????

Bob LearnedCommented:
1) You don't care what the assembly name is.
2) The class only needs to implement an interface
3) Look for files in the App_Code folder
4) Test to see if the DLL assembly implements the interface:

Imports System.Reflection
Imports System.IO

Public Class InterfaceTester

  Public Function FindInterface(ByVal filePath As String, ByVal interfaceName As String) As Boolean

    For Each fileName As String In Directory.GetFiles(filePath, "*.dll")
      Dim assy As [Assembly] = [Assembly].LoadFrom(fileName)

      For Each definedType As Type In assy.GetTypes()

        If definedType.Name = interfaceName Then
          Return True
        End If

      Next definedType
    Next fileName

    Return False

  End Function  'FindInterface

End Class


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.