在cad中,如果用源命令netload加载dll,会把当前dll占用,在VS中修改代码无法重新生成dll,需要关闭cad才行。现重新编写一个netloadx功能,打开cad后,用netload加载netloadx后,后续用netloadx加载dll,就可以实现在VS中修改代码,无需关闭cad即可加载重新生成的dll。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using System.Windows.Forms;
using System.Reflection;
namespace netloadx
{
//本程序在AutoCAD的快捷命令是“NX”
public class Netloadx
{
[CommandMethod("nx")]
public void netloadx()
{
//AutoCAD命令栏
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
//调用Windows.Forms选择一个文件
System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
//判断确认按钮
if (fileDialog.ShowDialog() == DialogResult.OK)
{
//选择的文件路径
string file_dir = fileDialog.FileName;
//在AutoCAD命令栏输出选择的文件路径
ed.WriteMessage("文件路径:" + file_dir);
//打开文件,将文件以二进制方式复制到内存,自动关闭文件
byte[] buffer = System.IO.File.ReadAllBytes(file_dir);
//加载内存中的文件
Assembly assembly = Assembly.Load(buffer);
}
}
}
}
在需要重新加载生成的dll中,需要进行两个设置;1、在Properties下面的Assemblyinfo.cs中,取消注释倒数第三行的代码,注释倒数第二行的代码。2、右键项目,在文件资源管理器中打开文件下,在主文件下下的.csproj文件中,将<Deterministic>后面的值改为false。