Copy Values From One Object to Another

  • Thread starter Thread starter GM
  • Start date Start date
G

GM

Hi,

I have 2 objects A and B. I want to copy only the values of B across to A
without changing the reference of A. Is there a way to achieve this without
the use of reflection?

Cheers,

Graham
 
GM,

You don't have to use reflection. You should just have a method that
takes an instance of your type, and sets the values internally.

Hope this helps.
 
Thanks for the reply. I should have explained it a bit better though.

I am trying to generalise how I do this so that the method for doing this
takes in 2 objects eg -
ApplyValues(object A, object B);
but the class containing this method does not care what the underlying type
of the objects are. So what I would really like to be able to do would be
to write something like:
Object.ApplyValues(object A, object B);

but I don't believe such a method exists.

If I wrote a method on each of my types that took another instance of that
type and set the values internally I'm not sure how I could cast the objects
to that type in order to call that method. Perhaps I need some kind of
interface which just contains an ApplyValues(method) implemented in my
custom types?

Thanks again,

Graham

Nicholas Paldino said:
GM,

You don't have to use reflection. You should just have a method that
takes an instance of your type, and sets the values internally.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

GM said:
Hi,

I have 2 objects A and B. I want to copy only the values of B across to A
without changing the reference of A. Is there a way to achieve this without
the use of reflection?

Cheers,

Graham
 
Back
Top