/// <summary> /// Ensures that the type follows command class rules /// </summary> /// <returns></returns> /// <exception cref="CommandParsingException">Thrown if the class does not conform to command class style rules</exception> public void VerifyClassAttribute() { CommandClassAttribute attr = _type.GetTypeInfo().GetCustomAttribute<CommandClassAttribute>(); if (attr == null) { throw new CommandParsingException( ParserFailReason.IncorrectType, $"Command '{_type.Name}' does not display a '{nameof(CommandClassAttribute)}'"); } }
public void ShowCommandsListStart() { // search through the current assembly for CommandMethods Assembly currentAssembly = Assembly.GetExecutingAssembly(); object[] customAttributes = currentAssembly.GetCustomAttributes(typeof (CommandClassAttribute), true); Type[] exportedTypes; int typesCount = customAttributes.Length; if (typesCount > 0) { exportedTypes = new Type[typesCount]; for (int i = 0; i < typesCount; i++) { CommandClassAttribute commandClassAttribute = customAttributes[i] as CommandClassAttribute; if (commandClassAttribute != null) { exportedTypes[i] = commandClassAttribute.Type; } } } else { exportedTypes = currentAssembly.GetExportedTypes(); } int index = 0; StringBuilder stringBuilder = new StringBuilder(null); foreach (Type type in exportedTypes) { MethodInfo[] meths = type.GetMethods(); foreach (MethodInfo meth in meths) { customAttributes = meth.GetCustomAttributes(typeof (CommandMethodAttribute), true); foreach (object obj in customAttributes) { CommandMethodAttribute commandMethodAttribute = (CommandMethodAttribute) obj; stringBuilder.Append("\n " + (index++) + " : " + commandMethodAttribute.GlobalName + " => " + meth.Name); } } } // MessageBox.Show(stringBuilder.ToString()); _ed.WriteMessage(stringBuilder.ToString()); _logger.Info(MethodBase.GetCurrentMethod().Name); }
/// <summary> /// Loading Enesy Command Method (ECM) information in this assembly /// </summary> /// <param name="markedOnly">Default value: false</param> internal void LoadingECM(bool markedOnly) { // Write message to command window Autodesk.AutoCAD.EditorInput.Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager. MdiActiveDocument.Editor; ed.WriteMessage("\nLoading ECM Database ..."); // Just get the commands for this assembly Assembly asm = Assembly.GetExecutingAssembly(); // for command category object[] categorys = asm.GetCustomAttributes(typeof(CommandGroup), true); Type[] catTypes; int catNumtypes = categorys.Length; // Get EnesyCommandMethod attributes object[] objs = asm.GetCustomAttributes(typeof(EnesyCADCommandMethod), true); Type[] tps; int numTypes = objs.Length; if (numTypes > 0) { tps = new Type[numTypes]; for (int i = 0; i < numTypes; i++) { CommandClassAttribute cca = objs[i] as CommandClassAttribute; if (cca != null) { tps[i] = cca.Type; } } } else { // If we're only looking for specifically marked CommandClasses, then use an // empty list if (markedOnly) { tps = new Type[0]; } else { tps = asm.GetExportedTypes(); } } // Append valid value into Database foreach (Type tp in tps) { MethodInfo[] meths = tp.GetMethods(); foreach (MethodInfo meth in meths) { objs = meth.GetCustomAttributes(typeof(EnesyCADCommandMethod), true); foreach (object obj in objs) { EnesyCADCommandMethod attb = (EnesyCADCommandMethod)obj; if (!attb.IsTest) { // get command category object[] cats = meth.GetCustomAttributes(typeof(CommandGroup), true); string category = ""; int index = 0; foreach (object c in cats) { index++; CommandGroup commandCat = c as CommandGroup; //GLOBAL.WriteMessage("Command " + attb.GlobalName + " - Category: " + commandCat.Category); category = commandCat.Category; } //GLOBAL.WriteMessage("\nCategory " + index + ""); // CmdRecord cmd = new CmdRecord(attb.GlobalName, attb.Tag, attb.Description, attb.Author, attb.Email, attb.WebLink, category ); // Check if Database contains this cmd if (!this.CmdTableRecord.Contains(attb.GlobalName)) { this.CmdTableRecord.Add(attb.GlobalName, attb.Tag, !String.IsNullOrEmpty(attb.Tag) ? "[" + attb.Tag + "] " + GetCommandDescription(attb.Description) : GetCommandDescription(attb.Description), attb.Author, attb.Email, attb.WebLink, category ); //if (!String.IsNullOrEmpty(cat.Category)) //{ // GLOBAL.WriteMessage("Category: " + cat.Category); //} } } } } } // }
/// <summary> /// Get information of commands from [EnesyCADCommandMethod] attribute /// </summary> /// <param name="asm">Assembly that contains .NET function</param> /// <param name="markedOnly"></param> /// <returns></returns> private static List <CommandInfo> GetCommands(Assembly asm, bool markedOnly) { { //StringCollection sc = new StringCollection(); List <CommandInfo> cmdInfo = new List <CommandInfo>(); object[] objs = asm.GetCustomAttributes(typeof(EnesyCADCommandMethod), true); Type[] tps; int numTypes = objs.Length; if (numTypes > 0) { tps = new Type[numTypes]; for (int i = 0; i < numTypes; i++) { CommandClassAttribute cca = objs[i] as CommandClassAttribute; if (cca != null) { tps[i] = cca.Type; } } } else { // If we're only looking for specifically // marked CommandClasses, then use an // empty list if (markedOnly) { tps = new Type[0]; } else { tps = asm.GetExportedTypes(); } } foreach (Type tp in tps) { MethodInfo[] meths = tp.GetMethods(); foreach (MethodInfo meth in meths) { objs = meth.GetCustomAttributes(typeof(EnesyCADCommandMethod), true); foreach (object obj in objs) { EnesyCADCommandMethod attb = (EnesyCADCommandMethod)obj; if (!attb.IsTest) { cmdInfo.Add(new CommandInfo(attb.GlobalName, attb.Tag, attb.Description, attb.Author, attb.Email, attb.WebLink ) ); } } } } return(cmdInfo); } }