Monday, September 29, 2008

Clone Controls, C# Sample

This post explains about creating clone objects using C#.

For example, you may need a grid with some standard design to repeat dynamically. You can use the below code to create the clone of the required controls or objects.

private object CloneControls(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;
}

No comments: