Author Topic: List unmarked commands only  (Read 2046 times)

0 Members and 1 Guest are viewing this topic.

kaefer

  • Guest
List unmarked commands only
« on: January 10, 2011, 11:41:16 AM »
Starting point was Kean's post from almost 4 years ago, http://through-the-interface.typepad.com/through_the_interface/2007/03/getting_the_lis.html#comments.

I tried to modify it in a way that lists the loaded assemblies in which CommandMethodAttributes are defined, but do not have CommandClassAttributes. For our internal development I have yet to see the need to adorn an assembly with a CommandClassAttribute. Why would I want to do this?

F# source follows...
Code: [Select]
module ListLoadedCommands

open Autodesk.AutoCAD.EditorInput
open Autodesk.AutoCAD.Runtime

type acApp = Autodesk.AutoCAD.ApplicationServices.Application

let CCAtype = typeof<CommandClassAttribute>
let CMAtype = typeof<CommandMethodAttribute>
let toCCA (o: obj) = o :?> CommandClassAttribute
let toCMA (o: obj) = o :?> CommandMethodAttribute

let typeToCommandMethods (tp: System.Type) =
    tp.GetMethods()
    |> Array.collect (fun mi -> mi.GetCustomAttributes(CMAtype, true))

let typeArrayToCommandMethodNames =
    Array.collect typeToCommandMethods
    >> Array.map (fun o -> (toCMA o).GlobalName)

[<CommandMethod "LLC">]
let listLoadedCommandsCmd() =
    let ed = acApp.DocumentManager.MdiActiveDocument.Editor
   
    let asms = System.AppDomain.CurrentDomain.GetAssemblies()
    for asm in asms do
       
        let cmdsMarked =
            asm.GetCustomAttributes(CCAtype, true)
            |> Array.map (fun o -> (toCCA o).Type)
            |> typeArrayToCommandMethodNames

        let cmdsUnmarked =
            asm.GetExportedTypes()
            |> typeArrayToCommandMethodNames
            |> Array.filter
                (fun cmd ->
                    match Array.tryFind ((=) cmd) cmdsMarked with
                    | Some _ -> false
                    | None -> true )

        if not(Array.isEmpty cmdsUnmarked) then
            ed.WriteMessage(
                "\n{0}\n{1}\n{2}\n ",
                asm.ManifestModule.Name,
                asm.FullName,
                System.String.Join(", ", cmdsUnmarked) )
Cheers,  Thorsten

dan.glassman

  • Guest
Re: List unmarked commands only
« Reply #1 on: January 10, 2011, 12:36:49 PM »
It's a load-time performance optimization.

If you don't include it, all classes are scanned for [CommandMethod]s.  If you do, only the listed ones are scanned.

Marginal utility for small modules.

-drg

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: List unmarked commands only
« Reply #2 on: January 10, 2011, 12:46:30 PM »
Starting point was Kean's post from almost 4 years ago, http://through-the-interface.typepad.com/through_the_interface/2007/03/getting_the_lis.html#comments.

< .. > Cheers,  Thorsten

wow, time sure flies :)
I'd forgotten about that.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.