C# 泛型方法New泛型对象

 

var frm = Activator.CreateInstance<T>();

 

/// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pnlFrm"></param>
        /// <param name="isQuery"> true调用查询方法</param>
        /// <returns></returns>
        public static bool SetTopUIForm<T>(UIPanel pnlFrm, bool isQuery = true) where T : UIForm
        {
            try
            {
                MsgBox.ShowWaitForm();
                UIForm frm = null;
                foreach (var s in pnlFrm.Controls)
                {
                    if (s is T)
                    {
                        frm = s as UIForm;
                        frm.Visible = true;
                    }
                    else
                    {
                        var f = s as UIForm;
                        f.Visible = false;
                    }
                }
                if (frm == null)
                {
                    frm = Activator.CreateInstance<T>();
                }
                if(isQuery)
                {
                    IQuery iq = frm as IQuery;
                    iq?.QueryMain();
                }
                frm.TopMost = true;
                frm.TopLevel = false;
                frm.ShowTitle = false;
                frm.FormBorderStyle = FormBorderStyle.None;
                frm.Dock = DockStyle.Fill;
                frm.Parent = pnlFrm;
                frm.Show();
                MsgBox.HideWaitForm();
                //pnlFrm.Controls.Clear();

            }
            catch (Exception ex)
            {
                MsgBox.ShowError(ex.Message);
            }
            finally
            {
                MsgBox.HideWaitForm();
            }
            return true;
        }

 

posted @ 2024-02-07 11:30  博客YS  阅读(38)  评论(0编辑  收藏  举报