?
Solved

finding current namespace in runtime

Posted on 2006-04-18
21
Medium Priority
?
302 Views
Last Modified: 2013-11-19
Hello

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
0
Comment
Question by:Darth_helge
  • 8
  • 7
  • 3
18 Comments
 
LVL 6

Expert Comment

by:cubixSoftware
ID: 16478250
hi

to get the assembly name...


dim asm as system.reflection.assembly

messagebox.show("My assembly name is " & asm.fullname)
0
 
LVL 5

Author Comment

by:Darth_helge
ID: 16478431
that gives me the value of 'nothing'
0
 
LVL 6

Expert Comment

by:cubixSoftware
ID: 16478565
mmm... try this

        ' Get our assembly.
        Dim executing_assembly As System.Reflection.Assembly = _
            Me.GetType.Assembly.GetEntryAssembly()
        ' Get our namespace.
        Dim executing_namespace As String = _
            executing_assembly.GetName().Name.ToString()



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


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:Darth_helge
ID: 16478732
darn...

i still get nothing reference for executing_assembly

i have to point out that this is a web application, not a windows forms application
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16478813
What kind of magic are you looking to perform with loading a dynamic assembly?

Bob
0
 
LVL 6

Expert Comment

by:cubixSoftware
ID: 16478844
crashed and burned.... that code snippet was from a winForm application.

If you are dealing with web then I think I'll bail out ;)
0
 
LVL 5

Author Comment

by:Darth_helge
ID: 16479376
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.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16479406
When you put the DLL in the bin folder on the web server, and call Assembly.LoadFrom("..\bin\DLLFile.dll"), does it work?

Bob
0
 
LVL 5

Author Comment

by:Darth_helge
ID: 16479480
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.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16479527
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?

Bob
0
 
LVL 5

Author Comment

by:Darth_helge
ID: 16479705
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()
a.createinstance("MyRoleProvider",p1,p2,p3)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16479721
Are you developing using something like Web Matrix, or the IDE?

Bob
0
 
LVL 5

Author Comment

by:Darth_helge
ID: 16479780
i use vs.net 2005
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16479932
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?

Bob
0
 
LVL 5

Author Comment

by:Darth_helge
ID: 16480030
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16480082
May I be so bold as to ask what you are trying to accomplish here?

Bob
0
 
LVL 5

Author Comment

by:Darth_helge
ID: 16485494

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... ?????

0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 16487556
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

Bob


0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question