c# - 创建 ASP.NET Control 对象的副本

标签 c# asp.net

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

最佳答案

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

/// <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;
}

关于c# - 创建 ASP.NET Control 对象的副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5555674/

相关文章:

asp.net - 为什么在asp.net中sql查询前要写@?

asp.net - 如何使用基于数据表的单元格重复 Tablix

javascript - 最小 SharePoint 页面中 window.open 后的 session 为空

c# - 无法确定条件表达式的类型?

c# - Entity Framework 和 Oracle,Linq where 子句字符串比较在给定函数参数而不是文字时不返回任何记录

c# - 监听另一个应用程序的退出

c# - 如何在不接收空字符串的情况下获取主板ID?

c# - 选择始终将程序集生成为 native 镜像

c# - 重定向到 ASP CORE 中的 URL

c# - 如何在 telerik C# 的 OnItemCreated 事件中获取列的值