Beispiel #1
0
        private void InitComponents(Assembly a)
        {
            string interfacename = typeof(IGenComponent).FullName;

            Type[] types = a.GetTypes();
            foreach (Type t in types)                           // 找出所有符合 IGenComponent 接口的类声明并创建加入集合
            {
                List <Type> interfaces = new List <Type>(t.GetInterfaces());
                if (interfaces.Exists(delegate(Type type) { return(type.FullName == interfacename); }))
                {
                    IGenComponent igc = (IGenComponent)a.CreateInstance(t.FullName);
                    if (igc.Properties.ContainsKey(GenProperties.IsEnabled) && igc.Properties[GenProperties.IsEnabled] == "False")
                    {
                        continue;
                    }
                    _gens.Add(igc);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// 实现鼠标右键上下文菜单
        /// </summary>
        private void _TreeView_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (_TreeView.GetNodeAt(e.X, e.Y) != null)
                {
                    TreeNode node = _TreeView.SelectedNode = _TreeView.GetNodeAt(e.X, e.Y);

                    ContextMenu cm      = new ContextMenu();
                    object      tag     = node.Tag;
                    Type        tagType = tag.GetType();

                    if (typeof(Database) == tagType)
                    {
                        // 找出针对 Database 生成的 IGenComponents
                        List <IGenComponent> gens = _gens.FindAll(delegate(IGenComponent o) { return((int)(o.TargetSqlElementType & SqlElementTypes.Database) > 0); });

                        // 将 IGenComponent 转为生成上下文菜单
                        foreach (IGenComponent gen in gens)
                        {
                            // 初始化一下 IGenComponent
                            IGenComponent g = gen;                                                      // 解决闭包问题
                            g.Server   = _server;
                            g.Database = (Database)tag;

                            // 如果通过生成前检查, 则生成上下文菜单, 并绑定生成事件
                            if (g.Validate())
                            {
                                CreateContextMenu(cm, (string)g.Properties[GenProperties.Group], (string)g.Properties[GenProperties.Caption], delegate(object s, EventArgs ea)
                                {
                                    Output(g.Gen());
                                });
                            }
                        }
                    }
                    else if (typeof(Table) == tagType)
                    {
                        List <IGenComponent> gens = _gens.FindAll(delegate(IGenComponent o) { return((int)(o.TargetSqlElementType & SqlElementTypes.Table) > 0); });
                        foreach (IGenComponent gen in gens)
                        {
                            IGenComponent g = gen;
                            g.Server   = _server;
                            g.Database = (Database)node.Parent.Parent.Tag;
                            if (g.Validate((Table)tag))
                            {
                                CreateContextMenu(cm, (string)g.Properties[GenProperties.Group], (string)g.Properties[GenProperties.Caption], delegate(object s, EventArgs ea)
                                {
                                    Output(g.Gen((Table)tag));
                                });
                            }
                        }
                    }
                    else if (typeof(UserDefinedTableType) == tagType)
                    {
                        List <IGenComponent> gens = _gens.FindAll(delegate(IGenComponent o) { return((int)(o.TargetSqlElementType & SqlElementTypes.UserDefinedTableType) > 0); });
                        foreach (IGenComponent gen in gens)
                        {
                            IGenComponent g = gen;
                            g.Server   = _server;
                            g.Database = (Database)node.Parent.Parent.Tag;
                            if (g.Validate((UserDefinedTableType)tag))
                            {
                                CreateContextMenu(cm, (string)g.Properties[GenProperties.Group], (string)g.Properties[GenProperties.Caption], delegate(object s, EventArgs ea)
                                {
                                    Output(g.Gen((UserDefinedTableType)tag));
                                });
                            }
                        }
                    }
                    else if (typeof(Microsoft.SqlServer.Management.Smo.View) == tagType)
                    {
                        List <IGenComponent> gens = _gens.FindAll(delegate(IGenComponent o) { return((int)(o.TargetSqlElementType & SqlElementTypes.View) > 0); });
                        foreach (IGenComponent gen in gens)
                        {
                            IGenComponent g = gen;
                            g.Server   = _server;
                            g.Database = (Database)node.Parent.Parent.Tag;
                            if (g.Validate((Microsoft.SqlServer.Management.Smo.View)tag))
                            {
                                CreateContextMenu(cm, (string)g.Properties[GenProperties.Group], (string)g.Properties[GenProperties.Caption], delegate(object s, EventArgs ea)
                                {
                                    Output(g.Gen((Microsoft.SqlServer.Management.Smo.View)tag));
                                });
                            }
                        }
                    }
                    else if (typeof(Column) == tagType)
                    {
                        List <IGenComponent> gens = _gens.FindAll(delegate(IGenComponent o) { return((int)(o.TargetSqlElementType & SqlElementTypes.Column) > 0); });
                        foreach (IGenComponent gen in gens)
                        {
                            IGenComponent g = gen;
                            g.Server   = _server;
                            g.Database = (Database)node.Parent.Parent.Tag;
                            if (g.Validate((Column)tag))
                            {
                                CreateContextMenu(cm, (string)g.Properties[GenProperties.Group], (string)g.Properties[GenProperties.Caption], delegate(object s, EventArgs ea)
                                {
                                    Output(g.Gen((Column)tag));
                                });
                            }
                        }
                    }
                    else if (typeof(StoredProcedure) == tagType)
                    {
                        List <IGenComponent> gens = _gens.FindAll(delegate(IGenComponent o) { return((int)(o.TargetSqlElementType & SqlElementTypes.StoredProcedure) > 0); });
                        foreach (IGenComponent gen in gens)
                        {
                            IGenComponent g = gen;
                            g.Server   = _server;
                            g.Database = (Database)node.Parent.Parent.Tag;
                            if (g.Validate((StoredProcedure)tag))
                            {
                                CreateContextMenu(cm, (string)g.Properties[GenProperties.Group], (string)g.Properties[GenProperties.Caption], delegate(object s, EventArgs ea)
                                {
                                    Output(g.Gen((StoredProcedure)tag));
                                });
                            }
                        }
                    }
                    else if (typeof(UserDefinedFunction) == tagType)
                    {
                        List <IGenComponent> gens = _gens.FindAll(delegate(IGenComponent o) { return((int)(o.TargetSqlElementType & SqlElementTypes.UserDefinedFunction) > 0); });
                        foreach (IGenComponent gen in gens)
                        {
                            IGenComponent g = gen;
                            g.Server   = _server;
                            g.Database = (Database)node.Parent.Parent.Tag;
                            if (g.Validate((UserDefinedFunction)tag))
                            {
                                CreateContextMenu(cm, (string)g.Properties[GenProperties.Group], (string)g.Properties[GenProperties.Caption], delegate(object s, EventArgs ea)
                                {
                                    Output(g.Gen((UserDefinedFunction)tag));
                                });
                            }
                        }
                    }
                    else if (typeof(ExtendedProperty) == tagType)
                    {
                        List <IGenComponent> gens = _gens.FindAll(delegate(IGenComponent o) { return((int)(o.TargetSqlElementType & SqlElementTypes.ExtendedProperty) > 0); });
                        foreach (IGenComponent gen in gens)
                        {
                            IGenComponent g = gen;
                            g.Server   = _server;
                            g.Database = (Database)node.Parent.Parent.Tag;
                            if (g.Validate((ExtendedProperty)tag))
                            {
                                CreateContextMenu(cm, (string)g.Properties[GenProperties.Group], (string)g.Properties[GenProperties.Caption], delegate(object s, EventArgs ea)
                                {
                                    Output(g.Gen((ExtendedProperty)tag));
                                });
                            }
                        }
                    }
                    else if (typeof(string) == tagType)
                    {
                        string name = (string)tag;

                        if (name == "Databases")
                        {
                            List <IGenComponent> gens = _gens.FindAll(delegate(IGenComponent o) { return((int)(o.TargetSqlElementType & SqlElementTypes.Databases) > 0); });
                            foreach (IGenComponent gen in gens)
                            {
                                IGenComponent g = gen;
                                g.Server   = _server;
                                g.Database = null;
                                if (g.Validate())
                                {
                                    CreateContextMenu(cm, (string)g.Properties[GenProperties.Group], (string)g.Properties[GenProperties.Caption], delegate(object s, EventArgs ea)
                                    {
                                        Output(g.Gen());
                                    });
                                }
                            }
                        }
                        else if (name == "Tables")
                        {
                            List <IGenComponent> gens = _gens.FindAll(delegate(IGenComponent o) { return((int)(o.TargetSqlElementType & SqlElementTypes.Tables) > 0); });
                            foreach (IGenComponent gen in gens)
                            {
                                IGenComponent g = gen;
                                g.Server   = _server;
                                g.Database = (Database)node.Parent.Tag;
                                if (g.Validate())
                                {
                                    CreateContextMenu(cm, (string)g.Properties[GenProperties.Group], (string)g.Properties[GenProperties.Caption], delegate(object s, EventArgs ea)
                                    {
                                        Output(g.Gen());
                                    });
                                }
                            }
                        }
                        else if (name == "Views")
                        {
                            List <IGenComponent> gens = _gens.FindAll(delegate(IGenComponent o) { return((int)(o.TargetSqlElementType & SqlElementTypes.Views) > 0); });
                            foreach (IGenComponent gen in gens)
                            {
                                IGenComponent g = gen;
                                g.Server   = _server;
                                g.Database = (Database)node.Parent.Tag;
                                if (g.Validate())
                                {
                                    CreateContextMenu(cm, (string)g.Properties[GenProperties.Group], (string)g.Properties[GenProperties.Caption], delegate(object s, EventArgs ea)
                                    {
                                        Output(g.Gen());
                                    });
                                }
                            }
                        }
                        else if (name == "Stored Procedures")
                        {
                            List <IGenComponent> gens = _gens.FindAll(delegate(IGenComponent o) { return((int)(o.TargetSqlElementType & SqlElementTypes.StoredProcedures) > 0); });
                            foreach (IGenComponent gen in gens)
                            {
                                IGenComponent g = gen;
                                g.Server   = _server;
                                g.Database = (Database)node.Parent.Tag;
                                if (g.Validate())
                                {
                                    CreateContextMenu(cm, (string)g.Properties[GenProperties.Group], (string)g.Properties[GenProperties.Caption], delegate(object s, EventArgs ea)
                                    {
                                        Output(g.Gen());
                                    });
                                }
                            }
                        }
                        else if (name == "Functions")
                        {
                            List <IGenComponent> gens = _gens.FindAll(delegate(IGenComponent o) { return((int)(o.TargetSqlElementType & SqlElementTypes.UserDefinedFunctions) > 0); });
                            foreach (IGenComponent gen in gens)
                            {
                                IGenComponent g = gen;
                                g.Server   = _server;
                                g.Database = (Database)node.Parent.Tag;
                                if (g.Validate())
                                {
                                    CreateContextMenu(cm, (string)g.Properties[GenProperties.Group], (string)g.Properties[GenProperties.Caption], delegate(object s, EventArgs ea)
                                    {
                                        Output(g.Gen());
                                    });
                                }
                            }
                        }
                        else if (name == "UserDefinedDataTypes")
                        {
                            // todo
                        }
                        else if (name == "UserDefinedTableTypes")
                        {
                            List <IGenComponent> gens = _gens.FindAll(delegate(IGenComponent o) { return((int)(o.TargetSqlElementType & SqlElementTypes.UserDefinedTableTypes) > 0); });
                            foreach (IGenComponent gen in gens)
                            {
                                IGenComponent g = gen;
                                g.Server   = _server;
                                g.Database = (Database)node.Parent.Tag;
                                if (g.Validate())
                                {
                                    CreateContextMenu(cm, (string)g.Properties[GenProperties.Group], (string)g.Properties[GenProperties.Caption], delegate(object s, EventArgs ea)
                                    {
                                        Output(g.Gen());
                                    });
                                }
                            }
                        }
                        else if (name == "User Schema")
                        {
                            // todo
                        }
                    }

                    if (cm.MenuItems.Count > 0)
                    {
                        _TreeView.ContextMenu = cm;
                    }
                    else
                    {
                        _TreeView.ContextMenu = null;
                    }
                }
                else
                {
                    _TreeView.ContextMenu = null;
                }
            }
        }