//直接要调用的dll
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Reflection;
//用到了System,System.IO,System.Reflection
namespace LoadX
{
public class LoadX
{
private Action cmd;
[CommandMethod("DD")]
public void ReloadX()
{
string dllName = "CadBasic.dll";//生成的需要调用的文件名
string className = "CadBasic.cmd";//空间名.类名
string methodName = "CmdSum";//method名
var adapterFileInfo = new FileInfo(Assembly.GetExecutingAssembly().Location);
var targetFilePath = Path.Combine(adapterFileInfo.DirectoryName,dllName);
var targetAssembly = Assembly.Load(File.ReadAllBytes(targetFilePath));
var targetType = targetAssembly.GetType(className);
var targetMethod = targetType.GetMethod(methodName);
var targetObject = Activator.CreateInstance(targetType);
cmd = () => targetMethod.Invoke(targetObject,null);
try
{
cmd?.Invoke();
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message,"Tips");
}
}
}
public class CmdInfo
{
public string DllName { get; set; }
public string ClassName { get; set; }
public string MethodName { get; set; }
}
}
//功能的dll
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CadBasic
{
public class cmd
{
[CommandMethod("CmdHello")]
public void CmdHello()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("Hello World From VS C#");
System.Windows.Forms.MessageBox.Show("Hello World From VS C#(It's for Debugging)", "Info(Tips)");
}
[CommandMethod("CmdSum")]
public void CmdSum()
{
int sum = 0;
int max = 200;
for (int i = 1; i <= max; i++)
{
sum += i;
}
double divisor = 13;
double divide = sum / divisor;
System.Windows.Forms.MessageBox.Show(sum.ToString() + "\n" + divide, "Info(Tips)");
}
}
}