471,337 Members | 1,034 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

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

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

Jan 13 '06 #1
5 1499
"Mi************@gmail.com" <Mi************@gmail.com> wrote in
news:11**********************@g44g2000cwa.googlegr oups.com:
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
Jan 13 '06 #2
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 ?

Jan 13 '06 #3
On Fri, 13 Jan 2006 12:58:13 -0600, Mi************@gmail.com
<Mi************@gmail.com> wrote:
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/de...tiesTopic1.asp

--
Craig
Microsoft MVP - ASP/ASP.NET
Jan 13 '06 #4
"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

Jan 13 '06 #5
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.

Jan 13 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by James Thurley | last post: by
22 posts views Thread by Generic Usenet Account | last post: by
10 posts views Thread by Timothy | last post: by
17 posts views Thread by David C. Ullrich | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.