hi all:
i have 2 dlls. one is a.dll, the other is b.dll.
and a1 is one of the command in a.dll. b1 is one of the command in b.dll
now i netload a.dll and run the a1 command.
in a1 command method how to load b.dll and run b1 command?
please don't ask me why not netload a.dll and b.dll together…
thanks
swaywood
Solved! Go to Solution.
Solved by norman.yuan. Go to Solution.
I believe that Autocad will not recognize the command in the b.dll unless you netload it. That being said, every custom command in .NET has a corresponding method that goes with it. In your A.dll just reference the B.dll and then inside of the command in the A.dll just call the method that corresponds to the command in the B.dll. Autocad should load it and then run the method.
hi Brown,
thank you for your reply. in my case ,a.dll is not reference b.dll. i want to use 'System.Reflection' to load dll file.
swaywood
Hi,
What about using Editor.Command() method for A2015+ or a RunCommand() wrapper (for prior versions) ?
ed.Command("_netload", "b.dll"); ed.Command("b1");
i tried this codes, but not success yet. cad version is 2010
public void prjOnlineUpdate() { object obj = null; byte[] filesByte; Assembly assembly; Type type; try { string folder = Environment.CurrentDirectory; if (folder[folder.Length - 1] == '\\') { folder = folder.Remove(folder.Length - 1); } //filesByte = File.ReadAllBytes(folder + "//cmdtest.dll"); //assembly = Assembly.Load(filesByte); assembly = Assembly.LoadFrom(folder + "//cmdtest.dll"); type = assembly.GetType("cmdtest.Class1"); obj = System.Activator.CreateInstance(type); MethodInfo timerStart = type.GetMethod("test"); if (timerStart != null) { timerStart.Invoke(obj, null); } } catch (System.Exception) { } }
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; namespace cmdTest { public class Class1 { [CommandMethod("test")] public void test() { Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage("test is ok"); } } }
As you already mentioned, if the a.dll has reference to b.dll, then you do not need to "NETLOAD" b.dll, because when a.dll is netloaded, b.dll is also loaded because of the reference.
However, as you requested, you could load b.dll using System.Reflection whenever needed: Assembly.Load()/LoadFile()/LoadFrom() (there are subltle differences among the 3 loading methods that you need to pay attention). Assembly loaded this way is the same as loaded by Acad's "NETLOAD" command (and the IExtensionApplication will run upon loading). Better yet, before you load the DLL assembly, you not only make sure the dll is in the .NET loading location according to .NET loading poll-mechnism; but also check if the assembly has already been loaded, to which you can use AppDomain.CurrentDomain.GetAssemblies() to find out loaded .NET assemblies (by its fully qualified name/version...).
Fortunately, the Autodesk.AutoCAD.Runtime.ExtensionLoader class (available since Acad2013?) has Load()/IsLoaded to do exactly the things aforementioned in more simplfied way.
HTH.
Norman Yuan
hi yuan:
thank you for your reply. i tried 3 load methods, but allways did not get the type(type = null), then 'obj = System.Activator.CreateInstance(type);' got error. could you help me write a test solution based on the code i post? the 2 dll does not reference each other. thank you so much.
swaywood
Hi yuan:
Your words solve my problem, thanks again!
First I use 'ExtensionLoader' to load the dll. And use 'AppDomain.CurrentDomain.GetAssemblies()' to get the 'Assembly'. Then I can run the command method now.
By the way, the 'ExtensionLoader' class is available in acad2010.
swaywood