创建一个ASP的副本.. NET控件对象

本文关键字:副本 NET 控件 对象 ASP 一个 创建 | 更新日期: 2023-09-27 17:49:20

我有一个缓存控件对象形式的UserControl在asp.net缓存。我喜欢访问我的控件并复制它以自由地更改它,而不会对基本缓存控件产生任何影响。

创建一个ASP的副本.. NET控件对象

大家好我找到了我要找的代码。我把它放在这里,也许对你也有帮助。

/// <summary>
/// this method makes a copy of object as a clone function
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static object Clone_Control(object o)
{
    Type type = o.GetType();
    PropertyInfo[] properties = type.GetProperties();
    Object retObject = 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;
}

做你想做的事当然是可能的。这很简单。也许是这样的?

private static void ChangeMyControlTextFromCache(string controlName, string newText, Panel containingPanel)
{
    MyControl original = ViewState[controlName] as MyControl;
    if (original == null)
    {
        original = new MyControl();
        ViewState[controlName] = original;
    }    
    MyControl clone = CloneMyControl(original);
    clone.Text = newText;
    if (containingPanel.Children.Contains(original))
        containingPanel.Children.Remove(original);
    containingPanel.Children.Add(clone);
}
private static MyControl CloneMyControl(MyControl original)
{
    MyControl clone = new MyControl();
    clone.Text = original.Text;
    clone.SomeOtherProperty = original.SomeOtherProperty;
    return clone;
}

这是另一个克隆方法,也将与MONO工作。它克隆传递的控件和它的子控件。注意:即使克隆控件尚未添加到任何页面,也不能将其添加到其他页面(或在以后的回发中)。它似乎可以在Windows上工作,但在MONO上会崩溃,所以我想这通常是不应该做的。

public static Control CloneControl(Control c) {
    var clone = Activator.CreateInstance(c.GetType()) as Control;
    if (c is HtmlControl) {
        clone.ID = c.ID;
        foreach (string key in ((HtmlControl)c).Attributes.Keys)
            ((HtmlControl)clone).Attributes.Add(key, (string)((HtmlControl)c).Attributes[key]);
    }
    else {
        foreach (PropertyInfo p in c.GetType().GetProperties()) {
            // "InnerHtml/Text" are generated on the fly, so skip them. "Page" can be ignored, because it will be set when control is added to a Page.
            if (p.CanRead && p.CanWrite && p.Name != "InnerHtml" && p.Name != "InnerText" && p.Name != "Page") {
                try {
                    p.SetValue(clone, p.GetValue(c, p.GetIndexParameters()), p.GetIndexParameters());
                }
                catch {
                }
            }
        }
    }
    for (int i = 0; i < c.Controls.Count; ++i)
        clone.Controls.Add(CloneControl(c.Controls[i]));
    return clone;
}

为包含LiteralControl和ResourceBasedLiteralControl的页面尝试此操作。

public static System.Web.UI.Control CloneControl(Control c)
            {
                Type type = c.GetType();
                var clone = (0 == type.GetConstructors().Length) ? new Control() : Activator.CreateInstance(type) as Control;
                if (c is HtmlControl)
                {
                    clone.ID = c.ID;
                    AttributeCollection attributeCollection = ((HtmlControl)c).Attributes;
                    System.Collections.ICollection keys = attributeCollection.Keys;
                    foreach (string key in keys)
                    {
                        ((HtmlControl)c).Attributes.Add(key, (string)attributeCollection[key]);
                    }
                }else if(c is System.Web.UI.LiteralControl){
                    clone = new System.Web.UI.LiteralControl(((System.Web.UI.LiteralControl)(c)).Text);
                }
                else
                {
                    PropertyInfo[] properties = c.GetType().GetProperties();
                    foreach (PropertyInfo p in properties)
                    {
                        // "InnerHtml/Text" are generated on the fly, so skip them. "Page" can be ignored, because it will be set when control is added to a Page.
                        if (p.Name != "InnerHtml" && p.Name != "InnerText" && p.Name != "Page" && p.CanRead && p.CanWrite)
                        {
                            try
                            {
                                ParameterInfo[] indexParameters = p.GetIndexParameters();
                                p.SetValue(clone, p.GetValue(c, indexParameters), indexParameters);
                            }
                            catch
                            {
                            }
                        }
                    }
                }
                int cControlsCount = c.Controls.Count;
                for (int i = 0; i < cControlsCount; ++i)
                {
                    clone.Controls.Add(CloneControl(c.Controls[i]));
                }
                return clone;
            }

参考:动态Web控件、回发和视图状态

向上投票的答案对我不起作用。下面是我创建ASP副本的解决方案。. NET控件对象。

    // Copies the custom properties on the control. To use it, cast your object to it. 
    public static object CopyUserControl(object obj, object obj2)
    {
        Type type = obj.GetType();
        PropertyInfo[] properties = type.GetProperties();
        Type type2 = obj2.GetType();
        PropertyInfo[] properties2 = type.GetProperties();
        foreach (PropertyInfo property in properties)
        {
            foreach (PropertyInfo property2 in properties2)
            {
                // Match the loops and ensure it's not a non-custom property 
                if (property.Name == property2.Name && property.SetMethod != null && !property.SetMethod.IsSecuritySafeCritical)
                    property2.SetValue(obj2, property.GetValue(obj, null));
            }
        }
        return obj2;
    }

当您必须在具有不同html id的同一页面上复制ASCX控件时,它非常有用。用法:

// Copying myAscxControl1 onto myAscxControl2
myAscxControl2 = (ObjectNameToBeCasted)CopyUserControl(myAscxControl1, myAscxControl2);

它也应该适用于不同类型的对象。但是,要复制的属性名必须匹配。