November 2014

Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30            










« Two methods for selecting entities at a particular location | Main | Linking Circles, Part 1: Using .NET events to relate AutoCAD geometry »

November 22, 2006

Finding the location of a .NET module

A quick pre-Thanksgiving tip that came from an internal discussion today: how to find the location of a .NET module (meaning the currently executing assembly).

Two techniques were identified:

  1. Identify the current assembly by asking where one of its types is defined
  2. Use the Assembly.GetExecutingAssembly() to get the assembly from where the current code is executing

Here's the C# code showing the two techniques:

using System;

using System.Reflection;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;


namespace AssemblyLocationTest

{

  public class AssemblyCmds

  {

    [CommandMethod("LOC")]

    public void GetModuleLocation()

    {

      Editor ed =

        Application.DocumentManager.MdiActiveDocument.Editor;


      // First technique

      Type type = typeof(AssemblyCmds);

      Assembly asm1 = type.Assembly;

      ed.WriteMessage(

        "\nAssembly location (1) is: "

        + asm1.Location

      );


      // Second technique

      Assembly asm2 = Assembly.GetExecutingAssembly();

      ed.WriteMessage(

        "\nAssembly location (2) is: "

        + asm2.Location

      );

    }

  }

}

And here are the results, just to show they return the same thing:

Command: loc

Assembly location (1) is: C:\temp\MyAssembly.dll

Assembly location (2) is: C:\temp\MyAssembly.dll

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83452464869e200d83538be7053ef

Listed below are links to weblogs that reference Finding the location of a .NET module:

blog comments powered by Disqus

Feed/Share

10 Random Posts