コード例 #1
0
        private IEnumerable <ToolStripMenuItem> CreateTree(IEnumerable <TAdapter> adapters)
        {
            var result = new List <ToolStripMenuItem>();

            foreach (var adapter in adapters)
            {
                var attr = adapter.GetType().GetCustomAttributes(typeof(MenuStripExtensionAttribute), false).Cast <MenuStripExtensionAttribute>().FirstOrDefault();

                var ignoreEnc = adapter.GetType().GetCustomAttributes(typeof(IgnoreEncryptionAttribute), false).Any();
                var ignoreDec = adapter.GetType().GetCustomAttributes(typeof(IgnoreDecryptionAttribute), false).Any();

                if (ignoreDec && ignoreEnc)
                {
                    continue;
                }

                if (attr == null)
                {
                    var nodeItem = result.FirstOrDefault(x => x.Name == "Others");
                    if (nodeItem == null)
                    {
                        nodeItem = new ToolStripMenuItem("Others");
                        result.Add(nodeItem);
                    }

                    var cipherItem = new ToolStripMenuItem(adapter.Name);
                    _addItemDelegates(cipherItem, adapter, ignoreDec, ignoreEnc);

                    nodeItem.DropDownItems.Add(cipherItem);
                }
                else
                {
                    ToolStripItemCollection items        = null;
                    ToolStripMenuItem       internalItem = null;

                    foreach (var strip in attr.Strips)
                    {
                        if (items == null)
                        {
                            internalItem = result.FirstOrDefault(x => x.Name == strip);
                        }
                        else
                        {
                            var index = items.IndexByName(strip);
                            if (index < 0)
                            {
                                internalItem = null;
                            }
                            else
                            {
                                internalItem = items[index] as ToolStripMenuItem;
                            }
                        }

                        if (internalItem == null)
                        {
                            internalItem = new ToolStripMenuItem(strip)
                            {
                                Name = strip
                            };
                            if (items == null)
                            {
                                result.Add(internalItem);
                            }
                            else
                            {
                                items.Add(internalItem);
                            }
                        }

                        items = internalItem.DropDownItems;
                    }

                    _addItemDelegates(internalItem, adapter, ignoreDec, ignoreEnc);
                }
            }

            return(result);
        }