现在的位置: 首页 > 综合 > 正文

在AutoCAD中程序化加载.NET程序集的方法(1)

2013年10月15日 ⁄ 综合 ⁄ 共 1405字 ⁄ 字号 评论关闭

前面的文章已经介绍过,通过修改注册表可以实现.NET程序集随AutoCAD的启动自动加载,这两天Kean又介绍了一种程序化的加载方法:用.NET程序加载.NET程序集,用到了微软的核心类库mscorlib中的System.Reflection命令空间。加载时只要一个函数Assembly.LoadFrom()可以了,试了下速度还是很快地。下面转一下Kean的测试代码(C#):

    1 using Autodesk.AutoCAD.ApplicationServices;

    2 using Autodesk.AutoCAD.EditorInput;

    3 using Autodesk.AutoCAD.Runtime;

    4 using System.Reflection;

    5 namespace LoadModule

    6 {

    7     public class Commands

    8     {

    9         [CommandMethod("MNL")]

   10         static public void MyNetLoad()

   11         {

   12             Document doc =

   13               Application.DocumentManager.MdiActiveDocument;

   14             Editor ed = doc.Editor;

   15             PromptStringOptions pso =

   16               new PromptStringOptions(

   17                 "/n输入要加载的程序集全路径: "

   18               );

   19             pso.AllowSpaces = true;

   20             PromptResult pr = ed.GetString(pso);

   21             if (pr.Status != PromptStatus.OK)

   22                 return;

   23             try

   24             {

   25                 Assembly.LoadFrom(pr.StringResult);

   26             }

   27             catch (System.Exception ex)

   28             {

   29                 ed.WriteMessage(

   30                   "/n无法加载程序集{0}: {1}",

   31                   pr.StringResult,

   32                   ex.Message

   33                 );

   34             }

   35         }

   36     }

   37 }

  此方法给用户随时后台(不知不觉地)加载.NET程序集提供了一种可能,美中不足的是托管的程序集目前只能加载无法卸载,一次加载终身受用,直到你关闭AutoCAD为至,呵呵。

  Lisp也可以实现相似的功能,下一篇做详细介绍...

抱歉!评论已关闭.