Author Topic: using Namespaces  (Read 2238 times)

0 Members and 1 Guest are viewing this topic.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
using Namespaces
« on: July 18, 2023, 10:18:10 PM »
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.Helper

Code - C#: [Select]
  1. using System;
  2.  
  3. using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  4.  
  5. namespace NS.Helper
  6. {
  7.    public static class Recorder
  8.    {
  9.       public static int Counter { get; set; } = 0;    
  10.    }
  11. }
  12.  

custom assembly P1 : namespace NS.P1
has using NS.Helper; declaration

Code - C#: [Select]
  1. using Autodesk.AutoCAD.Runtime;
  2. using System.Reflection;
  3. using NS.Helper;
  4.  
  5. using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  6.  
  7. [assembly: CommandClass(typeof(NS.P1.Commands_P1))]
  8.  
  9. namespace NS.P1
  10. {
  11.    public class Commands_P1
  12.    {
  13.       [CommandMethod("P1_TEST")]
  14.       public static void Test_P1()
  15.       {
  16.          var ed = CadApp.DocumentManager.MdiActiveDocument.Editor;
  17.  
  18.          Recorder.Counter += 1;
  19.          MethodBase m = MethodBase.GetCurrentMethod();
  20.          string home = $"{m.ReflectedType.Namespace} . {m.ReflectedType.Name} . {m.Name} ";
  21.          ed.WriteMessage(
  22.             $"From in : {home} :: Counter is {Recorder.Counter}\n");
  23.          
  24.       }
  25.    }
  26. }
  27.  

custom assembly P2 : namespace NS.P2
has using NS.Helper; declaration

Code - C#: [Select]
  1. using Autodesk.AutoCAD.Runtime;
  2. using System.Reflection;
  3. using NS.Helper;
  4.  
  5. using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  6.  
  7. [assembly: CommandClass(typeof(NS.P2.Commands_P2))]
  8.  
  9. namespace NS.P2
  10. {
  11.    public class Commands_P2
  12.    {
  13.       [CommandMethod("P2_TEST")]
  14.       public static void Test_P2()
  15.       {
  16.          var ed = CadApp.DocumentManager.MdiActiveDocument.Editor;
  17.  
  18.          Recorder.Counter += 1;
  19.          MethodBase m = MethodBase.GetCurrentMethod();
  20.          string home = $"{m.ReflectedType.Namespace} . {m.ReflectedType.Name} . {m.Name} ";
  21.          ed.WriteMessage(
  22.             $"From in : {home} :: Counter is {Recorder.Counter}\n");
  23.       }
  24.    }
  25. }
  26.  

The static Property Counter in Helper can be accessed and modified by P1 and P2
. . . . which was the goal of the exercise.

Code: [Select]
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.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.