PDA

View Full Version : Recommendations for deep copying C# object



suityou01
17th September 2014, 20:10
Tried a few things but they all fall foul.

I am reading this article

Deep Copy of Object in C# (http://www.c-sharpcorner.com/UploadFile/ff2f08/deep-copy-of-object-in-C-Sharp/)

I tried the reflection method, but if throws an exception about an object not having an empty constructor, Forms.Cursor is the type.

The serialisation method just seems heavy to me, and I need performance. Perhaps I am wrong.

Anyone from the congregation done this? If so, best practices and some pointers would be gratefully received.

I know it's late, and this is only for plan B so take your time. :happy

suityou01
18th September 2014, 08:09
OK you can get a move on now. :D

d000hg
18th September 2014, 08:45
If performance might be an issue, I think you need to provide more details (deja vu) what the object looks like. Is it fundamentally a POD, albeit with sub-objects which are also POD, or does it contain elements which don't conceptually support copying in a neat fashion (like, I dunno, a Thread or something)?

suityou01
18th September 2014, 08:56
If performance might be an issue, I think you need to provide more details (deja vu) what the object looks like. Is it fundamentally a POD, albeit with sub-objects which are also POD, or does it contain elements which don't conceptually support copying in a neat fashion (like, I dunno, a Thread or something)?

If there was any funny business I would have said Doogie.

It's just a POD, with some PODs.

What's a POD btw? Is this the C hash equivalent of Pojo?

d000hg
18th September 2014, 09:00
If there was any funny business I would have said Doogie.Hmm


It's just a POD, with some PODs.

What's a POD btw? Is this the C hash equivalent of Pojo?It's a POD but you don't know what a POD is?! Plain Old Data (a C++ term)

How big is this object, by whatever metric you consider apt?

suityou01
18th September 2014, 11:10
Hmm

It's a POD but you don't know what a POD is?! Plain Old Data (a C++ term)

How big is this object, by whatever metric you consider apt?

Well as I said in the OP it is a plan B.

If I used sheer number of projects, and sheer number of classes and interfaces it's probably the biggest and most complicated thing I have ever ever worked on.

Why would I use a C++ term for a C hash object? The term here is POCO. Plain Old CLR Object - Wikipedia, the free encyclopedia (http://en.wikipedia.org/wiki/Plain_Old_CLR_Object)

So by the very definition of a POCO, it is not. It wraps a user control that inherits from Forms.Panel. It also inherits from an abstract class I created, which in turn inherits from a few interfaces.

Basically the object model is a thing of beauty.

Does any of this help?

GlenW
18th September 2014, 11:41
Have you tried serialising the object then restoring it to a different object?
[Sorry didn't read OP properly]

suityou01
18th September 2014, 11:50
Have you tried serialising the object then restoring it to a different object?
[Sorry didn't read OP properly]

That is where I'm thinking of going. It seemed crude when reflection is available, but I wanted a read on what the panel consider best practice for performance.

It probably took longer to type the explanation of having not read the OP properly than to read the OP properly :D

GlenW
18th September 2014, 11:55
That is where I'm thinking of going. It seemed crude when reflection is available, but I wanted a read on what the panel consider best practice for performance.

It probably took longer to type the explanation of having not read the OP properly than to read the OP properly :D
I'd give it a try, binary serialise to a byte[] then deserialise back to a different object, it may be more efficient than you would expect.

d000hg
18th September 2014, 12:43
Well as I said in the OP it is a plan B.

If I used sheer number of projects, and sheer number of classes and interfaces it's probably the biggest and most complicated thing I have ever ever worked on.What? I said how big is the object, not the project. i.e. does it boil down to a few key-value pairs or hundreds of kilobytes of data.


Why would I use a C++ term for a C hash object? The term here is POCO. Plain Old CLR Object - Wikipedia, the free encyclopedia (http://en.wikipedia.org/wiki/Plain_Old_CLR_Object)Because most OO programming concepts are common to most OO languages? POD is referenced from that very article btw.

jmo21
18th September 2014, 12:43
Any use? (http://stackoverflow.com/questions/78536/deep-cloning-objects)



var serialized = JsonConvert.SerializeObject(source);
return JsonConvert.DeserializeObject<T>(serialized);

suityou01
18th September 2014, 12:47
The problem is that my class wraps a user control. So all objects need to be serialisable.



Additional information: Type 'System.Windows.Forms.UserControl' in Assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.


And I think this sets me back to square one.

DimPrawn
18th September 2014, 20:37
Sounds like another Suity disaster in the making.

Good luck with that :happy

DimPrawn
18th September 2014, 20:40
Oh and

c# - Why are System.Windows.Forms.Control not marked as serializable? - Stack Overflow (http://stackoverflow.com/questions/9127253/why-are-system-windows-forms-control-not-marked-as-serializable)

suityou01
18th September 2014, 21:22
Sounds like another Suity disaster in the making.

Good luck with that :happy

So I implemented IClonable and wrote my own clone method. Disaster averted :smokin

Gittins Gal
21st September 2014, 21:21
JSON is a subset of YAML I think.

Churchill
22nd September 2014, 02:13
So I implemented IClonable and wrote my own clone method. Disaster averted :smokin

And you've just proved why C# is shite!

eek
22nd September 2014, 07:29
And you've just proved why C# is tulipe!

No, I think all he has done is shown that a poor workman blames his tools...

Churchill
22nd September 2014, 07:31
No, I think all he has done is shown that a poor workman blames his tools...

C# is still shite. Whether or not Suity makes a balls of it or not ;-)

d000hg
22nd September 2014, 10:50
And you've just proved why C# is tulipe!

Because it provides a handy interface to let you do this?

C# is pretty neat, generally speaking.

Unix
22nd September 2014, 10:53
Serialize then de-serialize using protobuf-net.

DimPrawn
23rd September 2014, 08:53
And you've just proved why C# is tulipe!

Why do you think that Spod?

Do you mean C# or .NET in general?



Personally I think Suity has proved Suity is shite :happy

Churchill
23rd September 2014, 10:09
Why do you think that Spod?

Do you mean C# or .NET in general?



Personally I think Suity has proved Suity is shite :happy

Firstly, Microsoft's half arsed attempt to produce a competitor to Java.

Windows itself is massively overweight and resource hungry.

d000hg
23rd September 2014, 10:10
Firstly, Microsoft's half arsed attempt to produce a competitor to Java.

Windows itself is massively overweight and resource hungry.

It's 2014, not 2004.

Churchill
23rd September 2014, 10:14
It's 2014, not 2004.

I was asked for my thoughts on the subject. **** off back to your "bible".

VectraMan
23rd September 2014, 10:16
Firstly, Microsoft's half arsed attempt to produce a competitor to Java.

Java itself is massively overweight and resource hungry.

FTFY.

.NET's done pretty well. You'd have to say it's at least whole arsed.

Unix
23rd September 2014, 10:18
FTFY.

.NET's done pretty well. You'd have to say it's at least whole arsed.

As a gravy train its been good. But my own preference would be a language like Go on Linux.

Churchill
23rd September 2014, 10:20
FTFY.

.NET's done pretty well. You'd have to say it's at least whole arsed.

Yep, it's a hefty enough toolkit. However, when I need to use it I prefer C++

Churchill
23rd September 2014, 10:22
As a gravy train its been good. But my own preference would be a language like Go on Linux.

It'll be interesting to see if "Swift" becomes more mainstream.

eek
23rd September 2014, 10:27
As a gravy train its been good. But my own preference would be a language like Go on Linux.

I suspect for a while javascript and node will be the winner. But its not the greatest system in the world.

I doubt swift will do well. Most people don't develop just for Apple...

Churchill
23rd September 2014, 10:32
I suspect for a while javascript and node will be the winner. But its not the greatest system in the world.

I doubt swift will do well. Most people don't develop just for Apple...

That's why said "mainstream".

Its concepts and toolsets are quite nifty and if it can cross over to Linux then it would be a start.

Unix
23rd September 2014, 10:36
I suspect for a while javascript and node will be the winner. But its not the greatest system in the world.

I doubt swift will do well. Most people don't develop just for Apple...

I hope not I tried out node and the code quickly descends into a mess of callbacks for anything non trivial.

eek
23rd September 2014, 10:37
I hope not I tried out node and the code quickly descends into a mess of callbacks for anything non trivial.

Yep, that's why people use promises. I would agree that without care node can within minutes become an unreadable unsupportable mess

Unix
23rd September 2014, 10:39
Yep, that's why people use promises. I would agree that without care node can within minutes become an unreadable unsupportable mess

I like go routines and channels they are build into the Go language.

DimPrawn
23rd September 2014, 10:46
Firstly, Microsoft's half arsed attempt to produce a competitor to Java.

Windows itself is massively overweight and resource hungry.

What is it about Java that makes it superior?

Windows Vista maybe, but Win 7 and 8 are pretty lean. Okay the Win 8 Metro GUI is shite, but the core OS is stable and very usuable.

I use this version, and it has been great Windows Embedded 8 Pro | Microsoft (http://www.microsoft.com/windowsembedded/en-gb/windows-embedded-8-pro.aspx)

d000hg
23rd September 2014, 10:50
It's 2014, not 2004.


I was asked for my thoughts on the subject. **** off back to your "bible".And I was responding to your thoughts, pointing out that they're entirely out of date. Sorry this isn't The Churchill Show.

Also, what's a "bible"?

Churchill
23rd September 2014, 10:57
What is it about Java that makes it superior?

Windows Vista maybe, but Win 7 and 8 are pretty lean. Okay the Win 8 Metro GUI is shite, but the core OS is stable and very usuable.

I use this version, and it has been great Windows Embedded 8 Pro | Microsoft (http://www.microsoft.com/windowsembedded/en-gb/windows-embedded-8-pro.aspx)

I don't like Java either.

eek
23rd September 2014, 12:44
We seem to have moved off the original topic here, which from memory was showing that Suity is clueless.

minestrone
23rd September 2014, 12:55
I don't like Java either.


I would image most people on this thread have not been recently canned from a permanent job.

So your technology likes and dislikes mean jack shit you thick ignorant uneducated twunt.

Churchill
23rd September 2014, 13:11
I would image most people on this thread have not been recently canned from a permanent job.

So your technology likes and dislikes mean jack tulip you thick ignorant uneducated twunt.

Your Mrs shagging around again?

For the record I've been at current client-co for over 18 months, have you sobered up at all during that time?

minestrone
23rd September 2014, 13:17
Your Mrs shagging around again?

For the record I've been at current client-co for over 18 months, have you sobered up at all during that time?

Was it really 18 months ago you got shit canned from a permanent job?

How time flies.

Churchill
23rd September 2014, 13:21
Was it really 18 months ago you got tulip canned from a permanent job?

How time flies.

Tempus fugit indeed! It's over 2 years but it must've seemed like yesterday to an alcoholic fuqwit like yourself.

Anyway, the little boy has woken up and we're going for a swim.

d000hg
23rd September 2014, 13:51
We seem to have moved off the original topic here, which from memory was showing that Suity is clueless.

Yes, now the thread is making Suity look good by comparison.