PC Review


Reply
Thread Tools Rate Thread

Creating copy of structure instance via reflection?

 
 
Larry Minton
Guest
Posts: n/a
 
      22nd May 2006
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
 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      24th May 2006
I guess the following post is relevant to your question, although not
exactly the same.
http://www.codeguru.com/columns/vb/article.php/c11859/
Use Interop Code and Overlap Fields with the Union Construct in VB .NET


"Larry Minton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
possible to copy Type instance to Type instance wo/ changing ref? not_a_commie Microsoft C# .NET 1 16th Aug 2007 08:13 PM
Using a structure as a property in a class gives error when accessing properties of structure D Witherspoon Microsoft Dot NET Framework Forms 6 5th Mar 2005 07:43 AM
Creating a subclass instance from a base class instance Larry Lard Microsoft VB .NET 2 28th Jan 2005 06:48 PM
MS Word Document File Structure -- Need Published Description of Structure Gray M. Strickland Microsoft Word Document Management 15 9th Nov 2004 07:24 PM
Structure declaration - use of structure Douglas Buchanan Microsoft Dot NET 3 12th Oct 2004 08:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:13 PM.