PC Review


Reply
Thread Tools Rate Thread

How to clone an object ?

 
 
=?Utf-8?B?QW5kcmV3?=
Guest
Posts: n/a
 
      5th Jun 2007
Hello, friends,

In c#.net, I need to clone an object passed into a method,

private object generateCollection(object userInfo)
{
//I then need to generate a group of userInfo,
//How can I clone this passed in object?
//Do we have syntax something like this?
//object newUserInfo = new typeof(userInfo);
}

Thanks a lot.
 
Reply With Quote
 
 
 
 
Chris Mullins [MVP]
Guest
Posts: n/a
 
      5th Jun 2007
Does the object in question support the IClonable interface?

If so, you can just call Clone().

If the interface isn't supported, you best bet is to go ahead (assuming it's
your code) and implement the interface. Other mechanisms such as trying to
Serialize and then Deserialize, will usually end up with some funky results.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

"Andrew" <(E-Mail Removed)> wrote in message
news:0D2E3119-DF53-41A2-8D34-(E-Mail Removed)...
> Hello, friends,
>
> In c#.net, I need to clone an object passed into a method,
>
> private object generateCollection(object userInfo)
> {
> //I then need to generate a group of userInfo,
> //How can I clone this passed in object?
> //Do we have syntax something like this?
> //object newUserInfo = new typeof(userInfo);
> }
>
> Thanks a lot.



 
Reply With Quote
 
Moty Michaely
Guest
Posts: n/a
 
      6th Jun 2007
On Jun 6, 1:11 am, Andrew <And...@discussions.microsoft.com> wrote:
> Hello, friends,
>
> In c#.net, I need to clone an object passed into a method,
>
> private object generateCollection(object userInfo)
> {
> //I then need to generate a group of userInfo,
> //How can I clone this passed in object?
> //Do we have syntax something like this?
> //object newUserInfo = new typeof(userInfo);
>
> }
>
> Thanks a lot.


Dear Andrew,

Are you asking if you can create an instance of typeof(userinfo) or
how you can clone the data?

There is no internal way to clone the userInfo object if it's not
implementing a way for doing it.
There are non trivial ways using memory manipulations but I wouldn't
suggest using those.

If you are asking if you could create an instance of it, you can use
the System.Runtime name space to achieve this, or using the
System.Activator class:
http://msdn2.microsoft.com/en-us/lib...activator.aspx

Again, I wouldn't suggest implementing it this way (by getting object
variables) since instantiating a class requires to be familiar with
the constructors of the class. The way the method is implemented can
cause many runtime exceptions since you can't predict the type of the
class.

It'd be best if you'd clarify your needs.

Moty.



 
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
Copy/Clone object tshad Microsoft C# .NET 1 26th Jan 2009 06:55 AM
Object Browser Clone =?Utf-8?B?UmV1YmVu?= Microsoft Dot NET 0 27th Aug 2006 03:01 AM
How to clone an object? Rob R. Ainscough Microsoft VB .NET 2 17th Dec 2005 01:11 PM
Clone an object? Alfred Salton Microsoft ASP .NET 1 28th Jun 2004 06:53 PM
Re: clone a object Fergus Cooney Microsoft C# .NET 2 8th Sep 2003 10:20 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:28 PM.