By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,356 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,356 IT Pros & Developers. It's quick & easy.

Deep cloning issue in C#.

P: 59
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
Share this Question
Share on Google+
3 Replies


Plater
Expert 5K+
P: 7,872
I think by nature of LinkedLists you have to spcifically copy it, like with: CopyTo() or something?
Nov 19 '07 #2

10K+
P: 13,264
....
public virtual Token Clone()
{
Token _lObjReturnToken = (Token)this.MemberwiseClone();
Type _lObjType = _lObjReturnToken.GetType();
FieldInfo[] _lObjFieldInfoArray = _lObjType.GetFields();

foreach (FieldInfo _lObjFieldInfo in _lObjFieldInfoArray)
{
object _lObjSourceFieldValue = _lObjFieldInfo.GetValue(this);

if (_lObjSourceFieldValue is Token)
{
Token _lObjSourceToken = (Token)_lObjSourceFieldValue;
Token _lObjClonedToken = _lObjSourceToken.Clone();
_lObjFieldInfo.SetValue(_lObjReturnToken, _lObjClonedToken);
}
}
return _lObjReturnToken;
}

.....
[/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
Expert 5K+
P: 7,872
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

Post your reply

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