Copying properties to 'this'

  • Thread starter Thread starter Adam W Root
  • Start date Start date
A

Adam W Root

I had this post that never got a response:

I have a base class A and a derived class B and need to create a new B
from an existing A:

A a = new A();
B b = new B(a);

I've thought about this:

A a = new A();
B b = B.GetFromA(a);

I only want a shallow copy, and I DON'T want to copy each and every
member by hand (classes change often). It seems like I should be able
to accomplish this by implementing IClonable or using MemberwiseClone
but haven't found the solution to downcasting the clone of A to a type
of B.

Try this on for size (I think this works except I don't know how to
implement the A(A a) constructor without copying each member):

public class A
{
... members ...

public A()
{
... initialize A members ...
}

public A(A a)
{
... clone 'a' to 'this' ...
}

... methods ...
}

public class B : A
{
... more members ...

public B(A a) : base(a)
{
... initialize B members ...
}

... more methods ...
}

Any ideas?? HELP!

Adam
 
Back
Top