Clone base-http://www.legalsoft.com.cn/docs/986.html

Source: Internet
Author: User
  1. /// <Summary>
  2. /// The baseobject class is an abstract class used for inheritance.
  3. /// Each class inherited from this class automatically supports the clone method.
  4. /// This class implements the icloneable interface, and each object inherited from this object will
  5. /// Supports the icloneable interface.
  6. /// </Summary>
  7. Public abstract class baseobject: icloneable
  8. {
  9. /// <Summary>
  10. /// Clone the object and return a reference to the cloned object
  11. /// </Summary>
  12. /// <Returns> reference A New Clone object </returns>
  13. Public object clone ()
  14. {
  15. // First, create an instance of the specified type
  16. Object newobject = activator. createinstance (this. GetType ());
  17. // Obtain the field array of the New Type instance.
  18. Fieldinfo [] fields = newobject. GetType (). getfields ();
  19. Int I = 0;
  20. Foreach (fieldinfo fi in this. GetType (). getfields ())
  21. {
  22. // Determine whether the field supports the icloneable interface.
  23. Type iclonetype = Fi. fieldtype. getinterface ("icloneable", true );
  24. If (iclonetype! = NULL)
  25. {
  26. // Obtain the icloneable interface of the object.
  27. Icloneable iclone = (icloneable) Fi. getvalue (this );
  28. // We use the clone method to set a new value for the field.
  29. Fields [I]. setvalue (newobject, iclone. Clone ());
  30. }
  31. Else
  32. {
  33. // If this field supports the icloneable interface, set it directly.
  34. Fields [I]. setvalue (newobject, Fi. getvalue (this ));
  35. }
  36. // Check whether the object supports the ienumerable interface. If yes,
  37. // We also need to enumerate all its items and check whether they support the ilist or idictionary interface.
  38. Type ienumerabletype = Fi. fieldtype. getinterface ("ienumerable", true );
  39. If (ienumerabletype! = NULL)
  40. {
  41. // Obtain the ienumerable interface of this field
  42. Ienumerable ienum = (ienumerable) Fi. getvalue (this );
  43. Type ilisttype = Fields [I]. fieldtype. getinterface ("ilist", true );
  44. Type idictype = Fields [I]. fieldtype. getinterface ("idictionary", true );
  45. Int J = 0;
  46. If (ilisttype! = NULL)
  47. {
  48. // Obtain the ilist interface.
  49. Ilist list = (ilist) fields [I]. getvalue (newobject );
  50. Foreach (Object OBJ in ienum)
  51. {
  52. // Check whether the current item supports the icloneable interface.
  53. Iclonetype = obj. GetType (). getinterface ("icloneable", true );
  54. If (iclonetype! = NULL)
  55. {
  56. // If the icloneable interface is supported,
  57. // We use it to set the object clone in the list.
  58. Icloneable clone = (icloneable) OBJ;
  59. List [J] = clone. Clone ();
  60. }
  61. // Note: if the items in the list do not support the icloneable interface
  62. // Items in the clone list will be the same as those in the original list
  63. // (As long as this type is a reference type)
  64. J ++;
  65. }
  66. }
  67. Else if (idictype! = NULL)
  68. {
  69. // Obtain the idictionary Interface
  70. Idictionary DIC = (idictionary) fields [I]. getvalue (newobject );
  71. J = 0;
  72. Foreach (dictionaryentry de in ienum)
  73. {
  74. // Check whether the current item supports the icloneable interface.
  75. Iclonetype = de. value. GetType ().
  76. Getinterface ("icloneable", true );
  77. If (iclonetype! = NULL)
  78. {
  79. Icloneable clone = (icloneable) De. value;
  80. Dic [de. Key] = clone. Clone ();
  81. }
  82. J ++;
  83. }
  84. }
  85. }
  86. I ++;
  87. }
  88. Return newobject;
  89. }
  90. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.