I've spent some time trying to understand how AutoCAD handles namespaces in various custom assemblies.
I'm really no closer to fully understanding it, but I have manages to satisfy my goal :
To have a helper Assembly that is accessable from other custom assemblies irrespective of the namespace used in each assembly.
This insures that I can use a static Property in a helper assembly and have it assessable for read and write from methods in assemblies with different namespaces.
I
'assume' that AutoCAD makes one in-memory 'copy' of the helper and addresses that when required by whoever has it referenced from their assembly. ( yet to be confirmed how it's done )
More Importantly, this works :
custom assembly Helper :
namespace NS.Helperusing System;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace NS.Helper
{
public static class Recorder
{
public static int Counter { get; set; } = 0;
}
}
custom assembly P1 :
namespace NS.P1has using NS.Helper; declaration
using Autodesk.AutoCAD.Runtime;
using System.Reflection;
using NS.Helper;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly
: CommandClass
(typeof(NS
.P1.Commands_P1))]
namespace NS.P1
{
public class Commands_P1
{
[CommandMethod("P1_TEST")]
public static void Test_P1()
{
var ed = CadApp.DocumentManager.MdiActiveDocument.Editor;
Recorder.Counter += 1;
MethodBase m = MethodBase.GetCurrentMethod();
string home = $"{m.ReflectedType.Namespace} . {m.ReflectedType.Name} . {m.Name} ";
ed.WriteMessage(
$"From in : {home} :: Counter is {Recorder.Counter}\n");
}
}
}
custom assembly P2 :
namespace NS.P2has using NS.Helper; declaration
using Autodesk.AutoCAD.Runtime;
using System.Reflection;
using NS.Helper;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly
: CommandClass
(typeof(NS
.P2.Commands_P2))]
namespace NS.P2
{
public class Commands_P2
{
[CommandMethod("P2_TEST")]
public static void Test_P2()
{
var ed = CadApp.DocumentManager.MdiActiveDocument.Editor;
Recorder.Counter += 1;
MethodBase m = MethodBase.GetCurrentMethod();
string home = $"{m.ReflectedType.Namespace} . {m.ReflectedType.Name} . {m.Name} ";
ed.WriteMessage(
$"From in : {home} :: Counter is {Recorder.Counter}\n");
}
}
}
The static Property
Counter in Helper can be accessed and modified by P1 and P2
. . . . which was the goal of the exercise.
COMMANDLINE
Command:
Command:
Command: netload Assembly file name: "NS.P1.dll"
Command:
NS.P1 loaded.
P1_TEST
From in : NS.P1 . Commands_P1 . Test_P1 :: Counter is 1
Command: P1_TEST
From in : NS.P1 . Commands_P1 . Test_P1 :: Counter is 2
Command: NETLOAD
Command:
NS.P2 loaded.
P2_TEST
From in : NS.P2 . Commands_P2 . Test_P2 :: Counter is 3
Command: P2_TEST
From in : NS.P2 . Commands_P2 . Test_P2 :: Counter is 4
Command: P1_TEST
From in : NS.P1 . Commands_P1 . Test_P1 :: Counter is 5
Command:
This shared data will, of course, only survive the current session.
. . . Unless it is serialised.
Stay Well.