473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Creating copy of structure instance via reflection?

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
May 22 '06 #1
1 1602
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" <av*****@jadeinc.com> wrote in message
news:sj********************************@4ax.com...
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

May 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Qwert | last post by:
Hello, why does the following code work for a CLASS, but not for a STRUCTURE? (The first MsgBox returns '0,0', but I expected it to be '100,0'.) (The second MsgBox returns 'Hello', as...
3
by: Chris Pratt | last post by:
I'm sorry to ask such a fundamental question, but is it possible to create a copy of an object in VB.Net 2005? I have an app which, on load, creates an instance of a class, into which it reads a...
9
by: Bill Grigg | last post by:
All, Can anyone supply an example or reference to an example of using reflection to determine the data types and array lengths contained in a nested stucture in C#? Actually, it is a structure...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.