473,546 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deep cloning issue in C#.

59 New Member
Hey guys. I have a little problem trying to clone my object, which is shown below. If you notice below, i am using a generic linked list structure to store byte arrays. The problem is when i use my Clone() method to clone this object, it doesn't clone the actual LinkedList. Any ideas on how to go about fixing this?
Much appreciated.
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Reflection;
  3. using System.Collections.Generic;
  4.  
  5. namespace Compiler
  6. {
  7.     public class Token : Interfaces.IToken
  8.     {
  9.         #region Private Declarations
  10.         private LinkedList<byte[]> _cTokenAddressList;
  11.         private int _cTokenIdentifier;
  12.         private int _cTokenLineNumber;
  13.         private int _cTokenColumnNumber;
  14.         private string _cTokenLiteralValue;
  15.         private TokenCollection _cTokenType;
  16.         #endregion
  17.  
  18.         #region Public Declarations
  19.         /// <summary>
  20.         /// Compiler.Token type enumerator.
  21.         /// </summary>
  22.         /// <remarks>Leaving enumerator as public for easy token type assignments.</remarks>
  23.         public enum TokenCollection
  24.         {
  25.             Identifier,
  26.             Number,
  27.             Keyword,
  28.             Operator,
  29.             Delimiter,
  30.             EOF,
  31.             Error
  32.         };
  33.         #endregion
  34.  
  35.         #region Constructor
  36.         public Token()
  37.         {
  38.             _cTokenAddressList = new LinkedList<byte[]>();
  39.         }
  40.         #endregion
  41.  
  42.         #region Public IToken Members
  43.         /// <summary>
  44.         /// Sets address location of type System.Byte struct.
  45.         /// </summary>
  46.         /// <param name="_pAddressLocation">Memory address of type System.Byte struct.</param>
  47.         public void SetAddress(byte[] _pAddressLocation)
  48.         {
  49.             if (!_cTokenAddressList.Contains(_pAddressLocation))
  50.                 _cTokenAddressList.AddFirst(_pAddressLocation);
  51.         }
  52.  
  53.         /// <summary>
  54.         /// Gets address of type System.Byte struct.
  55.         /// </summary>
  56.         /// <returns>Memory address of type System.Byte struct.</returns>
  57.         public byte[] GetAddress()
  58.         {
  59.             if (_cTokenAddressList.Count > 0)
  60.             {
  61.                 byte[] _lAddressNode = _cTokenAddressList.First.Value;
  62.                 _cTokenAddressList.RemoveFirst();
  63.                 return _lAddressNode;
  64.             }
  65.             return new byte[] { 0x00 };
  66.         }
  67.  
  68.         /// <summary>
  69.         /// Creates a deep clone of Compiler.Token object.
  70.         /// </summary>
  71.         /// <returns>Clone of Compiler.Token object.</returns>
  72.         public virtual Token Clone()
  73.         {
  74.             Token _lObjReturnToken = (Token)this.MemberwiseClone();
  75.             Type _lObjType = _lObjReturnToken.GetType();
  76.             FieldInfo[] _lObjFieldInfoArray = _lObjType.GetFields();
  77.  
  78.             foreach (FieldInfo _lObjFieldInfo in _lObjFieldInfoArray)
  79.             {
  80.                 object _lObjSourceFieldValue = _lObjFieldInfo.GetValue(this);
  81.  
  82.                 if (_lObjSourceFieldValue is Token)
  83.                 {
  84.                     Token _lObjSourceToken = (Token)_lObjSourceFieldValue;
  85.                     Token _lObjClonedToken = _lObjSourceToken.Clone();
  86.                     _lObjFieldInfo.SetValue(_lObjReturnToken, _lObjClonedToken);
  87.                 }
  88.             }
  89.             return _lObjReturnToken;
  90.         }
  91.  
  92.         /// <summary>
  93.         /// Sets/Gets Compiler.Token identifier.
  94.         /// </summary>
  95.         public int TokenIdentifier 
  96.         { 
  97.             get
  98.             {
  99.                 return _cTokenIdentifier;
  100.             }
  101.             set
  102.             {
  103.                 _cTokenIdentifier = value;
  104.             }
  105.         }
  106.  
  107.         /// <summary>
  108.         /// Sets/Gets Compiler.Token line number.
  109.         /// </summary>
  110.         public int TokenLineNumber
  111.         {
  112.             get
  113.             {
  114.                 return _cTokenLineNumber;
  115.             }
  116.             set
  117.             {
  118.                 _cTokenLineNumber = value;
  119.             }
  120.         }
  121.  
  122.         /// <summary>
  123.         /// Sets/Gets Compiler.Token column number.
  124.         /// </summary>
  125.         public int TokenColumnNumber
  126.         {
  127.             get
  128.             {
  129.                 return _cTokenColumnNumber;
  130.             }
  131.             set
  132.             {
  133.                 _cTokenColumnNumber = value;
  134.             }
  135.         }
  136.  
  137.         /// <summary>
  138.         /// Sets/Gets Compiler.Token literal value.
  139.         /// </summary>
  140.         public string TokenLiteralValue
  141.         {
  142.             get
  143.             {
  144.                 return _cTokenLiteralValue;
  145.             }
  146.             set
  147.             {
  148.                 _cTokenLiteralValue = value;
  149.             }
  150.         }
  151.  
  152.         /// <summary>
  153.         /// Sets/Gets Compiler.Token type.
  154.         /// </summary>
  155.         public TokenCollection TokenType
  156.         {
  157.             get
  158.             {
  159.                 return _cTokenType;
  160.             }
  161.             set
  162.             {
  163.                 _cTokenType = value;
  164.             }
  165.         }
  166.         #endregion
  167.     }
  168. }
  169.  
Nov 17 '07 #1
3 2066
Plater
7,872 Recognized Expert Expert
I think by nature of LinkedLists you have to spcifically copy it, like with: CopyTo() or something?
Nov 19 '07 #2
r035198x
13,262 MVP
....
public virtual Token Clone()
{
Token _lObjReturnToke n = (Token)this.Mem berwiseClone();
Type _lObjType = _lObjReturnToke n.GetType();
FieldInfo[] _lObjFieldInfoA rray = _lObjType.GetFi elds();

foreach (FieldInfo _lObjFieldInfo in _lObjFieldInfoA rray)
{
object _lObjSourceFiel dValue = _lObjFieldInfo. GetValue(this);

if (_lObjSourceFie ldValue is Token)
{
Token _lObjSourceToke n = (Token)_lObjSou rceFieldValue;
Token _lObjClonedToke n = _lObjSourceToke n.Clone();
_lObjFieldInfo. SetValue(_lObjR eturnToken, _lObjClonedToke n);
}
}
return _lObjReturnToke n;
}

.....
[/code]
Your Token's clone method is calling clone again? Aren't you running into an infinite loop with this?
Nov 19 '07 #3
Plater
7,872 Recognized Expert Expert
Your Token's clone method is calling clone again? Aren't you running into an infinite loop with this?
You managed to quote yourself r0, lol
Nov 19 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
5422
by: Kevin | last post by:
Hi all, I've got a PHP4 app that I developed which I'm trying to get to run on a PHP5 server. Everything works great, except for one thing. There's a particular routine that creates an original object, then copies it. (The object constructor gets some meta information from the database, so I copy it for performance reasons). The routine...
13
2173
by: ahaupt | last post by:
Hi all, I'm implementing the Clone() method through the ICloneable interface and don't quite know how deep I need to go for a deep copy. Example: class A: ICloneable { object _val;
1
2329
by: oDDskOOL | last post by:
I realized today that the Hashtable.Clone only produces a shallow copy... that makes me go mad that M$ doesn't even provide a deep copy ctor for the Hashtable class ! mighty tech ducks might reply "oh but what if the types in hastable don't have copy ctors defined? that could lead to dangerous situations". well, but was it a significant...
1
1490
by: Neven Klofutar | last post by:
Hi, Can please someone send me a link or an example of deep cloning. thanx, Neven
5
25633
by: BenW | last post by:
Hello, What is the easiest way to make "deep copy" of my Hashtable? How about with other Collection classes in C#, any documents available? I don'r actually understand why Framework's Collection classes does not support deep copy methods by theself, only Clone with shallow copy.
2
10094
by: bonk | last post by:
I have come across the need to distinguish between the creation of a deep and a shallow copy and with great interest I have read this article: http://blogs.msdn.com/brada/archive/2004/05/03/125427.aspx This artivle seems to hint that I should not use System.IClonable but instead define my own interface(s) for cloning. Now since this...
22
15913
by: Steven Blair | last post by:
I need to perform a Deep Copy on an ArrayList. I wrote a small sample app to prove this could be done: ArrayList a = new ArrayList(); ArrayList b = new ArrayList(); a.Add("Hello"); b = (ArrayList) a.Clone();
3
8741
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word format, 3 pp) My question, coming from a C++ background where deep copying is done, is why in C# you would do either deep or shallow copying as...
9
13286
gits
by: gits | last post by:
This short article introduces a method that may be used to create a 'deep-copy' of an javascript object. You might ask: 'Wherefore do we need this?' ... Answer: 'Only variable-values of the basic data-types string, int, float, boolean and to make the confusion perfect :) functions too! are passed by value, all others are passed by reference.' This...
0
7435
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7694
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7792
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.