locked
Get Path of Assembly RRS feed

  • Question

  • Hi,

    I have two assemblies and my main application:

    - Application1

    - Assembly1

    - Assembly2

     

    In Assembly1 I have Class1 which implements Method1.

    In Assembly2 I have Class2 which derives from Class1.

     

    Application1 creates an instance of Class2 and calls Method1.

     

    In Method1 I need the path of the Assembly2. So I need the path of the Assembly which runs Class2.Method1.

     

    How can I get this path? I only get the path of Application1 and Assembly1 in Method1 :-(

     

    Regards,

    Sascha

    Tuesday, September 20, 2011 11:58 AM

Answers

  • Ok, I ended up with the following solution:

     

    Class1 has an abstract property "AssemblyPath", so Class2 in Assembly2 has to return the path of Assembly2 in the getter of AssemblyPath. Now Method1 can use the path from AssemblyPath.


    • Marked as answer by Sascha L Tuesday, September 20, 2011 3:15 PM
    • Edited by Sascha L Tuesday, September 20, 2011 3:16 PM
    Tuesday, September 20, 2011 3:15 PM
  • No need to reimplement it for each class. You need the assembly where the type of the current object is defined:

    this.GetType().Assembly

     

    • Marked as answer by Sascha L Tuesday, September 20, 2011 4:39 PM
    Tuesday, September 20, 2011 4:34 PM

All replies

  • Sascha,

    I don't see a way to be able to get the path of the assembly that called a method, inside the called method.  When another object has its members accessed it has no way of knowing what the calling application, or even which other object in its own assembly, was that called it. 

    You would have to accept the path of the other application as a defined parameter to the method in order to get this information. 


    Tom Overton
    Tuesday, September 20, 2011 12:16 PM
  • Sascha,

    I don't see a way to be able to get the path of the assembly that called a method, inside the called method.  When another object has its members accessed it has no way of knowing what the calling application, or even which other object in its own assembly, was that called it. 

    You would have to accept the path of the other application as a defined parameter to the method in order to get this information. 


    Tom Overton
    What Tom said x2: Accept a commandline argument use the args[] array.
    Tuesday, September 20, 2011 12:19 PM
  • The assembly which 'runs' Class2.Method1 is actually Assembly1, so that is what Assembly.GetExecutingAssembly will return.

    If you want to get Assembly2, I suggest marking Class1.Method1 as virtual, and overriding it in Class2. This may or may not be appropriate depending on your project.

    Tuesday, September 20, 2011 12:21 PM
  • Kendfrey,

    good suggestion and that would work, so now there is a new implementation of method1 defined in Assembly2, and GetExecutingAssembly would now return Assembly2.

    However what if there is a 3rd Assembly, Assembly3?  And that assembly creates an instance of Class2 and calls Method1.  I think Sasha is saying he would like Method1 to get the path of Assembly3.  I just don't think there is any way to get this, unless you were to loop through all the loaded assemblies in the domain during the method call:

    AppDomain MyDomain = AppDomain.CurrentDomain; 
    Assembly[] AssembliesLoaded = MyDomain.GetAssemblies(); 
    
    
    


     And then maybe by viewing the call stack you could figure it out.

    
    
    

     


    Tom Overton
    Tuesday, September 20, 2011 12:52 PM
  • Thanks to all!

     

    Yes, you are right, there will be also Assembly3, Assembly4, etc. which have Classes which are derived from Class1. So overiding the method makes no sense, because then I have to duplicate a lot of code. I thought Method1 will be also part of Class2, so if Method1 in Class2 is running, I thought it knows, that Class2 is in Assembly2, but it seems that Method1 is always running in Assembly1.

    Tuesday, September 20, 2011 1:32 PM
  • Ok, I ended up with the following solution:

     

    Class1 has an abstract property "AssemblyPath", so Class2 in Assembly2 has to return the path of Assembly2 in the getter of AssemblyPath. Now Method1 can use the path from AssemblyPath.


    • Marked as answer by Sascha L Tuesday, September 20, 2011 3:15 PM
    • Edited by Sascha L Tuesday, September 20, 2011 3:16 PM
    Tuesday, September 20, 2011 3:15 PM
  • No need to reimplement it for each class. You need the assembly where the type of the current object is defined:

    this.GetType().Assembly

     

    • Marked as answer by Sascha L Tuesday, September 20, 2011 4:39 PM
    Tuesday, September 20, 2011 4:34 PM
  • Thanks! This works! :-)
    Tuesday, September 20, 2011 4:39 PM