Hi Rob,
There may be better ways, but this may work
private void button1_Click(object sender, EventArgs e)
{
TextBox t = (TextBox)CloneObject(textBox1);
}
private object CloneObject(object o)
{
Type t = o.GetType();
PropertyInfo[] properties = t.GetProperties();
Object p = t.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, null, o, null);
foreach(PropertyInfo pi in properties)
{
if(pi.CanWrite)
{
pi.SetValue(p, pi.GetValue(o, null), null);
}
}
return p;
}
This code should create a new object of the same type and any writable property will get their values copied.
There may be far better ways though.
On Thu, 22 Feb 2007 02:37:56 +0100, Rob Stevenson <no_spam@_nospam.comwrote:
Does anyone know how to do this accurately. I really only want to clone the
design-time properties which should make the task easier. I've searched high
and low however and still can't find a problem-free solution. Even (ad-hoc)
solutions posted by MSFT employees have problems. For instance, if you
simply copy all serializable properties, you may eventually receive an
"Object does not match target type" exception. In my case, this occurswhen
I encounter (and clone) the "Location" property due to the fact that the
"Site" property was cloned earlier. If I clone "Location" first however
(before "Site"), it works fine. There must be a clean way to do this. Can
anyone offer any insight. Thanks.
--
Happy coding!
Morten Wennevik [C# MVP]