Exemplo n.º 1
0
        internal void RefreshTreeView(Dictionary <AddinManagerAssembly, List <IExcelExCommand> > nodesInfo)
        {
            if (nodesInfo != null)
            {
                if (nodesInfo.Comparer.GetType() != typeof(AssemblyComparer))
                {
                    throw new ArgumentException("The dictionary used to synchronize the treeview must have an \"AssemblyComparer\".");
                }

                //
                treeView1.Nodes.Clear();
                foreach (var ndInfo in nodesInfo)
                {
                    AddinManagerAssembly   asm     = ndInfo.Key;
                    List <IExcelExCommand> methods = ndInfo.Value;
                    // 添加新的程序集
                    TreeNode tnAss = new TreeNode(asm.Assembly.ManifestModule.ScopeName);
                    tnAss.Tag = asm;
                    treeView1.Nodes.Add(tnAss);
                    // 添加此程序集中所有的外部命令
                    foreach (IExcelExCommand m in methods)
                    {
                        TreeNode tnMethod = new TreeNode(m.GetType().FullName);
                        tnMethod.Tag = m;
                        tnAss.Nodes.Add(tnMethod);
                    }
                }

                // 刷新
                _nodesInfo = nodesInfo;
                //
                treeView1.ExpandAll();
                treeView1.Refresh();
            }
        }
Exemplo n.º 2
0
        private static Dictionary <AddinManagerAssembly, List <IExcelExCommand> > DeserializeAssemblies(
            AssemblyInfos amInfos)
        {
            var nodesInfo = new Dictionary <AddinManagerAssembly, List <IExcelExCommand> >(new AssemblyComparer());

            //
            if (amInfos != null)
            {
                foreach (string assemblyPath in amInfos.AssemblyPaths)
                {
                    if (File.Exists(assemblyPath))
                    {
                        // 将每一个程序集中的外部命令提取出来
                        List <IExcelExCommand> m = ExCommandFinder.RetriveExternalCommandsFromAssembly(assemblyPath);
                        if (m.Any())
                        {
                            Assembly             ass        = m[0].GetType().Assembly;
                            AddinManagerAssembly amAssembly = new AddinManagerAssembly(assemblyPath, ass);
                            nodesInfo.Add(amAssembly, m);
                        }
                    }
                }
            }
            return(nodesInfo);
        }
Exemplo n.º 3
0
        private void RunExternalCommand(TreeNode ndCommand)
        {
            var exCommand            = ndCommand.Tag as IExcelExCommand;
            AddinManagerAssembly asm = ndCommand.Parent.Tag as AddinManagerAssembly;
            //
            string assemblyPath = asm.Path;

            ExCommandExecutor.InvokeExternalCommand(assemblyPath, exCommand, _excelApplication);
        }
Exemplo n.º 4
0
        private void RemoveAssembly(TreeNode ndAss)
        {
            if (ndAss.Level != 0)
            {
                throw new ArgumentException("this is not a node representing an assembly.");
            }
            //
            AddinManagerAssembly asm = ndAss.Tag as AddinManagerAssembly;

            _nodesInfo.Remove(asm);
        }
Exemplo n.º 5
0
        private void RemoveMethod(TreeNode ndAss)
        {
            if (ndAss.Level != 1)
            {
                throw new ArgumentException("this is not a node representing an method.");
            }
            //
            AddinManagerAssembly asm = ndAss.Parent.Tag as AddinManagerAssembly;

            IExcelExCommand mtd = ndAss.Tag as IExcelExCommand;

            //
            _nodesInfo[asm].Remove(mtd);
        }
Exemplo n.º 6
0
        private void button_Reload_Click(object sender, EventArgs e)
        {
            TreeNode nd    = treeView1.SelectedNode;
            TreeNode ndAss = null;

            if (nd == null)
            {
                return;
            }
            //
            if (nd.Level == 0) // 移除程序集
            {
                ndAss = nd;
            }
            else if (nd.Level == 1)// 移除某个方法所对应的程序集
            {
                ndAss = nd.Parent;
            }
            AddinManagerAssembly mtd = ndAss.Tag as AddinManagerAssembly;
            string assFullPath       = mtd.Path;

            // 重新加载此程序集
            if (!string.IsNullOrEmpty(assFullPath))
            {
                bool hasNewMethodAdded = false;
                //
                var methods = ExCommandFinder.RetriveExternalCommandsFromAssembly(assFullPath);
                if (methods.Any())
                {
                    // 更新 Dictionary
                    AddMethodsInOneAssembly(assFullPath, methods);
                    hasNewMethodAdded = true;
                }

                if (hasNewMethodAdded)
                {
                    // 刷新界面
                    RefreshTreeView(_nodesInfo);
                }
            }
        }
Exemplo n.º 7
0
        /// <summary> 将从一个 Assembly 中加载进来的所有有效的外部命令同步到 _nodesInfo 中 </summary>
        /// <param name="methods"></param>
        private void AddMethodsInOneAssembly(string assemblyPath, List <IExcelExCommand> methods)
        {
            AddinManagerAssembly asm;

            if (methods.Any())
            {
                asm = new AddinManagerAssembly(assemblyPath, methods.First().GetType().Assembly);
                //
                List <IExcelExCommand> mds = new List <IExcelExCommand>();
                foreach (var m in methods)
                {
                    mds.Add(m);
                }
                //
                if (_nodesInfo.ContainsKey(asm))  // 覆盖式刷新
                {
                    _nodesInfo[asm] = mds;
                }
                else  // 直接进行添加就可以了
                {
                    _nodesInfo.Add(asm, mds);
                }
            }
        }