Creating copy of structure instance via reflection?

  • Thread starter Thread starter Larry Minton
  • Start date Start date
L

Larry Minton

I'm checking to see if there is an easy way to "clone" a structure
instance via reflection. I have a System::Object that boxes a
structure instance, and I want to create a new structure instance with
the same field values.

I can say something like:

Object ^ l_pObject = GetObject();
ICloneable ^ l_pICloneable = dynamic_cast<ICloneable^>(l_pObject);
if (l_pICloneable)
return intern(l_pICloneable->Clone());
// see if a structure.
Type ^ l_pType = GetType();
if (l_pType->IsValueType && !l_pType->IsPrimitive)
{
Object ^ l_pNewObject = Activator::CreateInstance(l_pType);
... add code here to get/set individual fields ...
return intern(l_pNewObject);
}
// can't clone
return this;

I can do the ".. add code here to get/set individual fields ...", but
it seems like there probably is an easier way.

Thanks, Larry
 
Back
Top