C# 深度克隆小组;控件不会重新绘制

C# 深度克隆小组;控件不会重新绘制,c#,reflection,controls,clone,redraw,C#,Reflection,Controls,Clone,Redraw,我在重新绘制克隆面板的子控件时遇到问题 首先,我没有使用IClonable。我用的是反射 我的代码: public static Panel ClonePanel(Panel panel) { Panel newPanel = (Panel) CloneControl(panel); foreach (Control ctl in panel.Controls) { Control newCtl = CloneControl(ctl);

我在重新绘制克隆面板的子控件时遇到问题

首先,我没有使用IClonable。我用的是反射

我的代码:

public static Panel ClonePanel(Panel panel)
{
    Panel newPanel = (Panel) CloneControl(panel);

    foreach (Control ctl in panel.Controls)
    {
        Control newCtl = CloneControl(ctl);
        newCtl.Visible = true;

        newPanel.Controls.Add(newCtl);
    }

    newPanel.Visible = true;

    return newPanel;
}

public static Control CloneControl(Control o)
{
    Type type = o.GetType();
    PropertyInfo[] properties = type.GetProperties();
    Control retObject = (Control) type.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, null, o, null);
    foreach (PropertyInfo propertyInfo in properties)
    {
        if (propertyInfo.CanWrite)
        {
            propertyInfo.SetValue(retObject, propertyInfo.GetValue(o, null), null);
        }
    }
    return retObject;
}

所以,我通过使用UserControl而不是Panel解决了这个问题,结果是非常非常好


另外,我唯一想做的是,不仅在特定的设计器中设计UserControl控件,而且在表单本身中设计UserControl控件。但这不是问题,我可以接受。

在第二期中,添加对System.Design的引用。 然后将[Deisgnertypeof]属性添加到用户控件中。这将使它在设计时起到面板控件的作用