locked
Help for getting assembly name using reflection RRS feed

  • Question

  • hello,

    I am not sure if i am posting it into correct forum

    I am building a VS package for getting function names, class name and other details whenever a person right clicks on any function. I am able to add the my command in Context menu and succeed in finding class name function name from where the command is called ie some other program. Now i want to get the assembly info of the program where this right click happened. I used System.Reflection.Assembly.GetExecutingAssembly().Location; System.Reflection.Assembly.GetAssembly().Location; but all of them returns the dll of the main project where the code of the call back is written not the assembly info form where the call has been made? is there any way to find this?

    I have class name with me from where the call is made

    Waiting for reply

    Thanks 

    Pranay

    Monday, June 21, 2010 7:38 PM

Answers

  • While its certainly possible, practicaly you're going to run into a few problems using reflection to do this. There are two problems, one is that if you use reflection to load an assembly, that assembly file and any dependents are now non-writable until your Process/App domain is shutdown. The second problem is that you will only be able to get info from the last successfull build (this can be mitigated by programmatically triggering the build and then using the result) 

     

    Problem one has a few solutions, one is to use a seperate app domain to load the dll, that way you can just throw it away and release the hold on the dll.

    Problem two can be solved by looking here http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/7717e61a-ae27-4f51-8fe1-06d07ab9ff92

    as for you original problem using Microsoft.Build.EvaluationProjectCollection.GlobalProjectCollection you can get all of the currently loaded projects. from there you can get the Project.Targets and from there you can get the Target.Output.

    Tuesday, June 22, 2010 1:35 AM

All replies

  • are you looking for the assembly of the program currently being edited?
    Monday, June 21, 2010 7:54 PM
  • what i mean is since it is a VS package i am looking for the assembly of the program where i am using this package, ie. When anyone use my package in any of his program and right click it i should get the assembly of the program where it is right clicked. i am able to extract class name of the program where it is right clicked I hope i made it clear
    Monday, June 21, 2010 7:59 PM
  • This is a visual studio package, so barring Isolated shell there is only devenv.exe. It still sounds to me like you're trying to get the compiled assembly of the project currently being edited by the instance of visual studio that has loaded your package, can you confirm this?
    Monday, June 21, 2010 8:17 PM
  • yes you are correct I am trying to get the compiled assembly of the project currently being edited by the instance of visual studio that has loaded my package
    Monday, June 21, 2010 8:30 PM
  • While its certainly possible, practicaly you're going to run into a few problems using reflection to do this. There are two problems, one is that if you use reflection to load an assembly, that assembly file and any dependents are now non-writable until your Process/App domain is shutdown. The second problem is that you will only be able to get info from the last successfull build (this can be mitigated by programmatically triggering the build and then using the result) 

     

    Problem one has a few solutions, one is to use a seperate app domain to load the dll, that way you can just throw it away and release the hold on the dll.

    Problem two can be solved by looking here http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/7717e61a-ae27-4f51-8fe1-06d07ab9ff92

    as for you original problem using Microsoft.Build.EvaluationProjectCollection.GlobalProjectCollection you can get all of the currently loaded projects. from there you can get the Project.Targets and from there you can get the Target.Output.

    Tuesday, June 22, 2010 1:35 AM
  • hi i went through your link to build a soultion programatically, but we need to pass project filename  parameter

     string projectFileName = @"...\ConsoleApplication3\ConsoleApplication3.sln";

    since in my case this can not be hard code, because i don't know the path of the application where my package will be used. So how should i go?

    there is no namespace like Microsoft.Build coming into my vs is these correct or something i am missing?  Can you tell more on 1st solution? 

    cant i get dll name using class name?

    Tuesday, June 22, 2010 6:44 AM
  • You will need to add a reference to Microsoft.Build.dll

    Please see the link below for info on loading and unloading assemblies using appdomains and reflection

    http://blogs.claritycon.com/blogs/steve_holstad/archive/2007/06/28/3212.aspx

    • Edited by JGreene Tuesday, June 22, 2010 5:27 PM added link
    Tuesday, June 22, 2010 2:45 PM
  • @nayna 

    can you describe what is the use of that vsproject? 

     

    @jgreene 

    i cant find any microsoft.build.dll but i found Microsoft.Build.Utilities etc things 

    Tuesday, June 22, 2010 6:05 PM
  • mine is physically located at C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Microsoft.Build.dll but its a reference assembly so it should show up in the add reference dialog under the .net tab
    Tuesday, June 22, 2010 7:18 PM
  • Use the following code:
    EnvDTE.Project currentProject = DTEObj.ActiveDocument.ActiveWindow.Project;

    VSLangProj.VSProject vsproject = (VSLangProj.VSProject)currentProject.Object;

    Properties properties = vsproject.Project.Properties;

    Iterate through each Property in the "properties".

    You can get the assembly name..

    Hope this helps..

    Regards,

    Nayna

    Thursday, June 24, 2010 9:43 AM
  • Hi Pranay,

    If you want to get Assembly Information of current project, you can use ProjectItem to look for AssemblyInfo file, and use FileCodeModel to get related information. For your reference, please refer to:

    #Managing AssemblyVersion I

    #MZ-Tools Articles Series: BUG: EnvDTE.Project.CodeModel doesn't retrieve attributes in the AssemblyInfo file

    Best Regards,
    Nancy


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, June 28, 2010 9:12 AM