"Mi************@gmail.com" <Mi************@gmail.com> wrote in
news:11**********************@g49g2000cwa.googlegr oups.com:
Thing is, I don't want an exact copy... just to copy the values of the
public properties. I want the values of the private variables to stay
the same. I probably should have mentioned that in the OP.
I can't for the life of me figure out why you would want to do this. First
of all, the state of the resulting object will likely be inconsistent.
Second, how do you know that some of the properties aren't read only?
Third, some might question the wisdom of "public properties" in the first
place.
If you are trying to do this just "to see if you can" then fine... its an
academic experiment. You can probably do something like this (untested):
MyClass m = new MyClass();
MyClass m2 = new MyClass();
foreach(PropertyInfo pi in m.GetType().GetProperties())
{
if (!pi.CanWrite || !pi.CanRead) continue;
PropertyInfo pi2 = m2.GetType().GetProperty(pi.Name);
pi2.SetValue(pi.GetValue());
}
-mdb