Can i retrieve all properties in a class to copy one object to another?

  • Thread starter Thread starter Michael.Suarez
  • Start date Start date
M

Michael.Suarez

Basically I want to do something like this (psuedo-code):


you have:
MyClass MyObject;
which is instantiated in the form load.


You have a copy function:
{
MyClass MyObjectCopy = new MyClass(id);


foreach (property p in MyClass.properties)
{
MyObject.p = MyObjectCopy.p;
}



}


This copy function obviously won't work as is. What would be the
correct way to simulate what I am trying to do here, or at least some
hints to get me started?

Thanks,
Mike
 
This copy function obviously won't work as is. What would be the
correct way to simulate what I am trying to do here, or at least some
hints to get me started?

If you just want to do this for MyClas (or other specific classes that you
define) then a better way would be to implement IClonable on the classes.

-mdb
 
Thanks for the response!

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.

That's why I thought the foreach way would be a good way to implement
this... the question is foreach ? in ?
 
Thanks for the response!

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.

That's why I thought the foreach way would be a good way to implement
this... the question is foreach ? in ?

look at System.Reflection classes....the thing to iterate over is gotten
by calling GetProperties():

http://msdn.microsoft.com/library/d...l/frlrfSystemTypeClassGetPropertiesTopic1.asp
 
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
 
Thank you very much!!

My new copy function:
{
MyClass MyObjectCopy = new MyClass(id);

foreach(PropertyInfo pi in MyObject.GetType().GetProperties())
{
if (pi.CanWrite)
{
pi.SetValue(MyObject, pi.GetValue(MyObjectCopy, null), null);
}
}
}
I can't for the life of me figure out why you would want to do this.

Basically, I have my own custom class. There are private variables with
identifiers. There are public read-only properties that are binded to
labels. There are other public properties binded to editable controls.
The user can open up a dialog box, select the id number of the thing
they want to copy from. The new object gets created. And now my
origional object's writeable properties are overwritten with the values
from the thing the user wanted to copy from.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top