class public members and recurse

  • Thread starter Thread starter Dmitri Shvetsov
  • Start date Start date
D

Dmitri Shvetsov

Hi,

Does anybody know if I can use something like recurse to get the whole list
of the public members of the class and their types/values?

For example, I created the object with default values, then I need to get
another object, created somewhere else, for example by serialization method,
then to assign all values from this external object to existing ones in my
object. I can do that using the members list, one by one. I suspect that
there is another one approach, more effective, like:

CMyClass MyObject = new CMyClass();
CMyClass DeserializedObject = Deserialize();
foreach (object obj in MyObject){
MyObject.obj = DeserializedObject.obj;
}//foreach

Is it possible at all?

Thanks,
Dmitri
 
Why don't you just use the instance that you have deserialized ? It is a
totally valid instance.
If, for some reason you cannot, you may consider implemnting a 'Clone'
method. Use the protected method memberwiseclone().
If you have no right onthe implementation of your class, you could use
Reflection to list all of the pulic properties, and then loop through them
(as you proposed).

Fred
 
Hi Fred,

Just because the serialization and deserialization are created as the
methods of the same class. I use these methods from the existing object. I
can't overwrite the instance of the object inside the same instance. That's
why I use this trick with a temporary additional object inside working one
and copy all members (pointers) from there. It's made in a basic class and
I'd like just to inherit these methods. It almost works, but I need to have
an ability to retrieve all members in a root class for all derived.

Thanks,
Dmitri Shvetsov
http://members.cox.net/dshvetsov/pictures.htm
 

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