protected Dictionary <CommandMethodAttribute, MethodInfo> GetCommandMethodsFromCurrentAssembly() { Type[] types = Assembly.GetExecutingAssembly().GetExportedTypes(); Dictionary <CommandMethodAttribute, MethodInfo> commandsAndMethodsDictionary = new Dictionary <CommandMethodAttribute, MethodInfo>(); foreach (Type type in types) { foreach (MethodInfo methodInfo in type.GetMethods()) { if (methodInfo == null) { continue; } if (methodInfo.Name.Contains("NotUsedDirectly")) { continue; } var methodsCustomAttributes = methodInfo.GetCustomAttributes(typeof(CommandMethodAttribute), true); if (methodsCustomAttributes.Length == 0) { continue; } CommandMethodAttribute commandMethodAttribute = methodInfo.GetCustomAttributes(typeof(CommandMethodAttribute), true)[0] as CommandMethodAttribute; if (commandMethodAttribute != null) { commandsAndMethodsDictionary.Add(commandMethodAttribute, methodInfo); } } } return(commandsAndMethodsDictionary); }
private void _pastToPanelAtSimpleRow(RibbonCommandButtonAttribute ribbAttr, CommandMethodAttribute cmdAttr, Type type, string id) { RibbonButton button = new RibbonButton(); button.IsToolTipEnabled = true; button.Id = type.FullName + id; button.Name = cmdAttr.GlobalName; button.Orientation = System.Windows.Controls.Orientation.Horizontal; button.ShowText = true; button.GroupName = ribbAttr.GroupName; button.CommandHandler = new RibbonButtonCommandHandler(); button.CommandParameter = cmdAttr.GlobalName; button.Size = RibbonItemSize.Standard; button.Text = ribbAttr.Name; button.GroupLocation = Autodesk.Private.Windows.RibbonItemGroupLocation.Last; string name = ribbAttr.GroupName; RibbonPanel panel = _ribbonTab.Panels.FirstOrDefault(p => p.Source.Title == name); if (panel == null) { panel = new RibbonPanel(); RibbonPanelSource source = new RibbonPanelSource(); source.Id = ribbAttr.GroupName + "___source_owner"; source.Title = button.GroupName; panel.Source = source; _ribbonTab.Panels.Add(panel); //panel.Source.IsSlideOutPanelVisible = true; panel.CanToggleOrientation = true; panel.ResizeStyle = RibbonResizeStyles.NeverHideText; //panel.CustomPanelTitleBarBackground = System.Windows.Media.Brushes.LightYellow; } RibbonItem row = panel.Source.Items.FirstOrDefault(x => { if (x is RibbonRowPanel) { if (((RibbonRowPanel)x).Items.Count / 2 < 3) { return(true); } } return(false); }); if (row == null) { row = new RibbonRowPanel(); row.Id = panel.Source.Id + "." + type.FullName + "_row"; row.Name = type.Name; panel.Source.Items.Add(row); } ((RibbonRowPanel)row).Items.Add(button); if (row is RibbonRowPanel) { ((RibbonRowPanel)row).Items.Add(new RibbonRowBreak()); } }
public static void GetCmdMethodInfo() { Assembly assem = Assembly.GetExecutingAssembly(); List <string> cmdsGlobal = new List <string>(); List <string> cmdsLocal = new List <string>(); List <string> cmdsGroup = new List <string>(); Module[] mods = assem.GetModules(true); foreach (Module mod in mods) { Type[] types = mod.GetTypes(); foreach (Type type in types) { ResourceManager rm = new ResourceManager(type.FullName, assem); rm.IgnoreCase = true; MethodInfo[] meths = type.GetMethods(); foreach (MethodInfo meth in meths) { object[] atts = meth.GetCustomAttributes(typeof(CommandMethodAttribute), true); foreach (object att in atts) { CommandMethodAttribute cmdAtt = (CommandMethodAttribute)att; if (cmdAtt != null) { string nameGlobal = cmdAtt.GlobalName; string nameLocal = nameGlobal; string nameLocalized = cmdAtt.LocalizedNameId; if (nameLocalized != null) { try { nameLocal = rm.GetString(nameLocalized); } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " ExtractMethods.cs: line: 48"); } } cmdsGlobal.Add(nameGlobal); cmdsLocal.Add(nameLocal); if (cmdAtt.GroupName != null && !cmdsGroup.Contains(cmdAtt.GroupName)) { cmdsGlobal.Add(cmdAtt.GroupName); } } } } } } }
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); }
internal MethodDescriptor(MethodInfo methodInfo) { var provider = CommandDescriptor.GetUsageDescriptionProvider(methodInfo.DeclaringType); this.methodInfo = methodInfo; this.attribute = methodInfo.GetCommandMethodAttribute(); this.name = attribute.Name != string.Empty ? attribute.Name : CommandSettings.NameGenerator(methodInfo.Name); this.displayName = methodInfo.GetDisplayName(); var switchList = new List<SwitchDescriptor>(); foreach (var item in methodInfo.GetParameters()) { var switchDescriptor = new SwitchParameterInfoDescriptor(item); switchList.Add(switchDescriptor); } var switchAttr = this.methodInfo.GetCustomAttribute<CommandMethodSwitchAttribute>(); if (switchAttr != null) { foreach (var item in switchAttr.PropertyNames) { var switchDescriptor = CommandDescriptor.GetSwitchDescriptors(methodInfo.DeclaringType)[item]; if (switchDescriptor == null) throw new ArgumentException(string.Format("'{0}' attribute does not existed .", item)); switchList.Add(switchDescriptor); } } var staticAttrs = this.methodInfo.GetCustomAttributes<CommandStaticSwitchAttribute>(); foreach (var item in staticAttrs) { var switches = CommandDescriptor.GetSwitchDescriptors(item.StaticType); switchList.AddRange(switches); } this.switches = switchList.Distinct().ToArray(); this.summary = provider.GetSummary(methodInfo); this.description = provider.GetDescription(methodInfo); }
public static void RegisterForDemandLoading() { // Get the assembly, its name and location Assembly assem = Assembly.GetExecutingAssembly(); string name = assem.GetName().Name; string path = assem.Location; // We'll collect information on the commands // (we could have used a map or a more complex // container for the global and localized names // - the assumption is we will have an equal // number of each with possibly fewer groups) List <string> globCmds = new List <string>(); List <string> locCmds = new List <string>(); List <string> groups = new List <string>(); // Iterate through the modules in the assembly Module[] mods = assem.GetModules(true); foreach (Module mod in mods) { // Within each module, iterate through the types Type[] types = mod.GetTypes(); foreach (Type type in types) { // We may need to get a type's resources ResourceManager rm = new ResourceManager(type.FullName, assem); rm.IgnoreCase = true; // Get each method on a type MethodInfo[] meths = type.GetMethods(); foreach (MethodInfo meth in meths) { // Get the methods custom command attribute(s) object[] attbs = meth.GetCustomAttributes(typeof(CommandMethodAttribute), true); foreach (object attb in attbs) { CommandMethodAttribute cma = attb as CommandMethodAttribute; if (cma != null) { // And we can finally harvest the information // about each command string globName = cma.GlobalName; string locName = globName; string lid = cma.LocalizedNameId; // If we have a localized command ID, // let's look it up in our resources if (lid != null) { // Let's put a try-catch block around this // Failure just means we use the global // name twice (the default) try { locName = rm.GetString(lid); } catch { } } // Add the information to our data structures globCmds.Add(globName); locCmds.Add(locName); if (cma.GroupName != null && !groups.Contains(cma.GroupName)) { groups.Add(cma.GroupName); } } } } } } // Let's register the application to load on demand (12) // if it contains commands, otherwise we will have it // load on AutoCAD startup (2) int flags = (globCmds.Count > 0 ? 12 : 2); // By default let's create the commands in HKCU // (pass false if we want to create in HKLM) CreateDemandLoadingEntries(name, path, globCmds, locCmds, groups, flags, true ); }
public void FindCmdDuplicates(string asmPath) { Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Assembly asm = Assembly.LoadFile(asmPath); Type[] expTypes; var errTypes = new Exception[0]; try { expTypes = asm.GetTypes(); } catch (ReflectionTypeLoadException ex) { errTypes = ex.LoaderExceptions; expTypes = ex.Types; } var map = new Dictionary <string, List <MethodInfo> >(); try { // foreach (Type type in expTypes) { if (type == null) { continue; } MethodInfo[] methods = type.GetMethods(); foreach (MethodInfo method in methods) { CommandMethodAttribute attribute = GetCommandMethodAttribute(method); if (attribute == null) { continue; } if (!map.ContainsKey(attribute.GlobalName)) { var methodInfo = new List <MethodInfo>(); map.Add(attribute.GlobalName, methodInfo); } map[attribute.GlobalName].Add(method); } } } catch (Exception ex) { MessageBox.Show(ex.Message + ex.StackTrace); } // 查看加载出错的类 foreach (Exception ex in errTypes) { ed.WriteMessage($"\n{ex.Message}"); } // 查看重新的类 foreach (var keyValuePair in map) { if (keyValuePair.Value.Count > 1) { ed.WriteMessage( "\nDuplicate Attribute: " + keyValuePair.Key); foreach (var method in keyValuePair.Value) { ed.WriteMessage( "\n – Method: " + method.Name); } } } }
/// <summary> /// 通过反射机制导出AutoCAD Net Dll的注册的命令 /// </summary> /// <param name="ass">包含AutoCAD Net Dll的注册的命令的Dll</param> /// <returns></returns> public List <myAutoCADNetDll> GetACADNetDllCmd(List <string> dllFileNames) { List <myAutoCADNetDll> listDlls = new List <myAutoCADNetDll>(); #region//收集自定义命令 try { foreach (var item in dllFileNames) { Assembly ass = Assembly.LoadFrom(item); List <myAutoCADNetCmd> ListCmds = new List <myAutoCADNetCmd>(); foreach (var t in ass.GetTypes()) { if (t.IsClass && t.IsPublic) { foreach (MethodInfo method in t.GetMethods()) { if (method.IsPublic && method.GetCustomAttributes(true).Length > 0) { CommandMethodAttribute cadAtt = null; myAutoCADcmdAttribute myAtt = null; foreach (var att in method.GetCustomAttributes(true)) { if (att.GetType().Name == typeof(CommandMethodAttribute).Name) { cadAtt = att as CommandMethodAttribute; } if (att.GetType().Name == typeof(myAutoCADcmdAttribute).Name) { myAtt = att as myAutoCADcmdAttribute; } } if (myAtt != null && cadAtt != null) { ListCmds.Add(new myAutoCADNetCmd(Path.GetFileNameWithoutExtension( ass.ManifestModule.Name.Substring(0, ass.ManifestModule.Name.Length - 4)), t.Name, method.Name, cadAtt.GlobalName, myAtt.HelpString, myAtt.IcoFileName)); } else { if (cadAtt != null && myAtt == null) { ListCmds.Add(new myAutoCADNetCmd(Path.GetFileNameWithoutExtension( ass.ManifestModule.Name.Substring(0, ass.ManifestModule.Name.Length - 4)), t.Name, method.Name, cadAtt.GlobalName, "", "")); } } } } } } if (ListCmds.Count > 0) { listDlls.Add(new myAutoCADNetDll(ass, ListCmds)); } } } catch (System.Exception ex) { Application.ShowAlertDialog(ex.Message); } #endregion return(listDlls); }
private CustomizationSection BuildMenu( XmlDocument kojtoCadMenuDefinition, Dictionary <CommandMethodAttribute, MethodInfo> commandsAndMethodsDictionary) { // rebuild the new cui // Create a customization section for our partial menu CustomizationSection kojtoCui = new CustomizationSection(); kojtoCui.MenuGroupName = Settings.Default.appName; // add a menu group var kojtoCadMacroGroup = new MacroGroup(kojtoCui.MenuGroupName, kojtoCui.MenuGroup); foreach (XmlNode toolBarDefinition in kojtoCadMenuDefinition.DocumentElement.ChildNodes) { var toolBar = new Toolbar(toolBarDefinition.Attributes["name"].Value, kojtoCui.MenuGroup); toolBar.ToolbarOrient = ToolbarOrient.right; toolBar.ToolbarVisible = ToolbarVisible.show; foreach (XmlNode buttonDefinition in toolBarDefinition) { foreach (KeyValuePair <CommandMethodAttribute, MethodInfo> commandAndMethod in commandsAndMethodsDictionary) { CommandMethodAttribute commandMethodAttribute = commandAndMethod.Key; MethodInfo method = commandAndMethod.Value; if (method.Name.Length <= "Start".Length) { continue; } string methodName = method.Name.Substring(0, method.Name.Length - "Start".Length); if (!methodName.Equals(buttonDefinition.Attributes["methodName"].Value, StringComparison.InvariantCultureIgnoreCase)) { continue; } MenuMacro menuMacro = new MenuMacro( kojtoCadMacroGroup, buttonDefinition.InnerText, commandMethodAttribute.GlobalName, method.Name); string smallImage = ""; string mediumImage = ""; ; try { smallImage = IconsManager.GetIconFile( commandAndMethod.Value.Name.Replace("Start", ""), IconSize.Small); mediumImage = IconsManager.GetIconFile( commandAndMethod.Value.Name.Replace("Start", ""), IconSize.Medium); menuMacro.macro.SmallImage = smallImage; menuMacro.macro.LargeImage = mediumImage; // The var toolBarButton is not assigned to anything because it needs only to be instantiated in order appear in the toolbar var toolBarButton = new ToolbarButton( menuMacro.ElementID, buttonDefinition.InnerText, toolBar, -1); } catch (System.Exception exception) { Logger.Error(Resources.ErrorCreatingToolbarButton, exception); } } } } return(kojtoCui); }
public List <CommandInfo> GetCommands(string filename) { var cmds = new List <CommandInfo>(); var ass = Assembly.LoadFrom(filename); try { foreach (Module m in ass.GetModules(true)) { Type[] types = ass.GetTypes(); foreach (Type t in types) { ResourceManager rm = new ResourceManager(t.FullName, ass); rm.IgnoreCase = true; MethodInfo[] methods = t.GetMethods(); foreach (MethodInfo mi in methods) { object[] attbs = mi.GetCustomAttributes( typeof(CommandMethodAttribute), true ); foreach (object attb in attbs) { CommandMethodAttribute cma = attb as CommandMethodAttribute; if (cma != null) { CommandInfo cmd = new CommandInfo(cma.GlobalName); cmd.TypeName = t.FullName; cmd.MethodName = mi.Name; cmd.LocalizedName = cmd.GlobalName; if (cma.GroupName != null) { cmd.GroupName = cma.GroupName; } string lnid = cma.LocalizedNameId; if (lnid != null) { try { cmd.LocalizedName = rm.GetString(lnid); } catch { } } cmds.Add(cmd); } } } } } return(cmds); } catch { } return(null); }