// Can actually move this somewhere else. // Retrieved from http://blog.spontaneouspublicity.com/associating-strings-with-enums-in-c public static string GetDescription(FlowMajorCategory value) { FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes != null && attributes.Length > 0) { return(attributes[0].Description); } else { return(value.ToString()); } }
// Can actually move this somewhere else. // Retrieved from http://blog.spontaneouspublicity.com/associating-strings-with-enums-in-c public static string GetDescription(FlowMajorCategory value) { var fi = value.GetType().GetField(value.ToString()); var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes != null && attributes.Length > 0) return attributes[0].Description; return value.ToString(); }
public FlowDesignerMajorCategoryAttribute(FlowMajorCategory majorcategory) { this.MajorCategory = majorcategory; }
private void CreateMenuItems(ContextMenuStrip menu) { // Get list of layer types. List <Type> types = new List <Type>(); foreach (var a in AppDomain.CurrentDomain.GetAssemblies()) { foreach (var t in a.GetTypes()) { if (typeof(IAlgorithm).IsAssignableFrom(t)) { types.Add(t); } } } // For each of those layer types, find ones that have // FlowDesignerName, FlowDesignerCategory and FlowDesignerMajorCategory attributes. List <SelectedType> selectedTypes = new List <SelectedType>(); foreach (var t in types) { bool foundName = false; bool foundCategory = false; string currentName = "<unknown>"; FlowCategory currentCategory = FlowCategory.Undefined; FlowMajorCategory currentMajorCategory = FlowMajorCategory.Undefined; object[] o = t.GetCustomAttributes(true); foreach (var a in o) { if (a is FlowDesignerNameAttribute) { currentName = (a as FlowDesignerNameAttribute).Name; foundName = true; } if (a is FlowDesignerCategoryAttribute) { currentCategory = (a as FlowDesignerCategoryAttribute).Category; foundCategory = true; } if (a is FlowDesignerMajorCategoryAttribute) { currentMajorCategory = (a as FlowDesignerMajorCategoryAttribute).MajorCategory; } } if (foundName && foundCategory) { selectedTypes.Add(new SelectedType { Name = currentName, MajorCategory = currentMajorCategory, Category = currentCategory, Type = t }); } } // Sort selected types into bins. selectedTypes.OrderBy(v => v.Name); menu.Items.Add(new ToolStripMenuItem("Tychaia World Generator") { Enabled = false }); foreach (FlowMajorCategory m in Enum.GetValues(typeof(FlowMajorCategory))) { bool cont = false; foreach (var t in selectedTypes) { if (t.MajorCategory.ToString() == m.ToString()) { cont = true; break; } } if (cont) { menu.Items.Add("-"); menu.Items.Add(new ToolStripMenuItem(FlowDesignerMajorCategoryAttribute.GetDescription(m) + ":") { Enabled = false }); foreach (FlowCategory c in Enum.GetValues(typeof(FlowCategory))) { cont = false; var cm = new ToolStripMenuItem(FlowDesignerCategoryAttribute.GetDescription(c)); foreach (var t in selectedTypes) { if (t.MajorCategory.ToString() == m.ToString() && t.Category.ToString() == c.ToString()) { cont = true; var tempt = t; cm.DropDownItems.Add(new ToolStripMenuItem(t.Name, null, (sender, ev) => { this.CreateDynamicLayer(tempt.Type); }, "c_" + t.Name)); } } if (cont) { menu.Items.Add(cm); } } } } // Add other options. if (selectedTypes.Count > 0) { menu.Items.Add("-"); } menu.Items.Add(this.c_DisableProcessingMenuItem); menu.Items.Add(this.c_ExportSelectedMenuItem); menu.Items.Add(this.c_AnalyseSelectedMenuItem); menu.Items.Add(this.c_TraceSelectedMenuItem); menu.Items.Add(this.c_RenameSelectedMenuItem); menu.Items.Add(this.c_DeleteSelectedMenuItem); }