Base class for cloning an object in C#

  1. /// <summary>
  2. /// BaseObject class is an abstract class for you to derive from.
  3. /// Every class that will be dirived from this class will support the
  4. /// Clone method automaticly.<br>
  5. /// The class implements the interface ICloneable and there
  6. /// for every object that will be derived <br>
  7. /// from this object will support the ICloneable interface as well.
  8. /// </summary>
  9.  
  10. public abstract class BaseObject : ICloneable
  11. {
  12. /// <summary>
  13. /// Clone the object, and returning a reference to a cloned object.
  14. /// </summary>
  15. /// <returns>Reference to the new cloned
  16. /// object.</returns>
  17. public object Clone()
  18. {
  19. //First we create an instance of this specific type.
  20. object newObject = Activator.CreateInstance( this.GetType() );
  21.  
  22. //We get the array of fields for the new type instance.
  23. FieldInfo[] fields = newObject.GetType().GetFields();
  24.  
  25. int i = ;
  26.  
  27. foreach( FieldInfo fi in this.GetType().GetFields() )
  28. {
  29. //We query if the fiels support the ICloneable interface.
  30. Type ICloneType = fi.FieldType.
  31. GetInterface( "ICloneable" , true );
  32.  
  33. if( ICloneType != null )
  34. {
  35. //Getting the ICloneable interface from the object.
  36. ICloneable IClone = (ICloneable)fi.GetValue(this);
  37.  
  38. //We use the clone method to set the new value to the field.
  39. fields[i].SetValue( newObject , IClone.Clone() );
  40. }
  41. else
  42. {
  43. // If the field doesn't support the ICloneable
  44. // interface then just set it.
  45. fields[i].SetValue( newObject , fi.GetValue(this) );
  46. }
  47.  
  48. //Now we check if the object support the
  49. //IEnumerable interface, so if it does
  50. //we need to enumerate all its items and check if
  51. //they support the ICloneable interface.
  52. Type IEnumerableType = fi.FieldType.GetInterface
  53. ( "IEnumerable" , true );
  54. if( IEnumerableType != null )
  55. {
  56. //Get the IEnumerable interface from the field.
  57. IEnumerable IEnum = (IEnumerable)fi.GetValue(this);
  58.  
  59. //This version support the IList and the
  60. //IDictionary interfaces to iterate on collections.
  61. Type IListType = fields[i].FieldType.GetInterface
  62. ( "IList" , true );
  63. Type IDicType = fields[i].FieldType.GetInterface
  64. ( "IDictionary" , true );
  65.  
  66. int j = ;
  67. if( IListType != null )
  68. {
  69. //Getting the IList interface.
  70. IList list = (IList)fields[i].GetValue(newObject);
  71.  
  72. foreach( object obj in IEnum )
  73. {
  74. //Checking to see if the current item
  75. //support the ICloneable interface.
  76. ICloneType = obj.GetType().
  77. GetInterface( "ICloneable" , true );
  78.  
  79. if( ICloneType != null )
  80. {
  81. //If it does support the ICloneable interface,
  82. //we use it to set the clone of
  83. //the object in the list.
  84. ICloneable clone = (ICloneable)obj;
  85.  
  86. list[j] = clone.Clone();
  87. }
  88.  
  89. //NOTE: If the item in the list is not
  90. //support the ICloneable interface then in the
  91. //cloned list this item will be the same
  92. //item as in the original list
  93. //(as long as this type is a reference type).
  94.  
  95. j++;
  96. }
  97. }
  98. else if( IDicType != null )
  99. {
  100. //Getting the dictionary interface.
  101. IDictionary dic = (IDictionary)fields[i].
  102. GetValue(newObject);
  103. j = ;
  104.  
  105. foreach( DictionaryEntry de in IEnum )
  106. {
  107. //Checking to see if the item
  108. //support the ICloneable interface.
  109. ICloneType = de.Value.GetType().
  110. GetInterface( "ICloneable" , true );
  111.  
  112. if( ICloneType != null )
  113. {
  114. ICloneable clone = (ICloneable)de.Value;
  115.  
  116. dic[de.Key] = clone.Clone();
  117. }
  118. j++;
  119. }
  120. }
  121. }
  122. i++;
  123. }
  124. return newObject;
  125. }
  126. }

Base class for cloning an object in C#的更多相关文章

  1. js原型链接(二)和object类的create方法

    原型链的内部执行方式 <script> function Myclass(){ this.x=" x in Myclass"; } var obj=new Myclas ...

  2. js Object.create 初探

    1.作用 Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__. https://developer.mozilla.org/zh-CN/docs/W ...

  3. [转]javascript之Object.assign()痛点

    本文转自:http://blog.csdn.net/waiterwaiter/article/details/50267787 最近也一直会用javascript,然后中间使用的一些组件,如Echar ...

  4. ECMAScript5之Object学习笔记(三)

    第三部分继续... Object.getOwnPropertyDescriptor(obj, prop) 获取一个对象的属性描述符 根据"Own"这个词我们可以猜到,prop只能是 ...

  5. 【javascript】base.js

    作为一个好的脚手架使用 /* Base.js, version 1.1a Copyright 2006-2010, Dean Edwards License: http://www.opensourc ...

  6. [C++] OOP - Base and Derived Classes

    There is a base class at the root of the hierarchy, from which the other class inherit, directly or ...

  7. c++ object model

    对一个结构体进行不断的封装后可以形成一个c++类,为此需要添加很多函数成员之类的代码,为此显示c++比c语言显得庞大并且迟缓,但是事实并不是这些 c++在布局和时间上的额外承担主要是由virtual引 ...

  8. jquery-1.11.1.js

       每次想要使用这个js时,总是要到官网上下载,太麻烦,现在把它收录了 jquery-1.11.1.js /*! * jQuery JavaScript Library v1.11.1 * http ...

  9. Game Development Patterns and Best Practices (John P. Doran / Matt Casanova 著)

    https://github.com/PacktPublishing/Game-Development-Patterns-and-Best-Practices https://github.com/m ...

随机推荐

  1. GN算法---《Community structure in social and biological networks》这篇论文讲了什么?

    用中文记下这篇论文的大致意思,以防止忘了.好记性不如烂笔头! 摘要:最近的一些研究在研究社交网络或WWW.研究者都集中于研究网络的“小世界性”,“幂率分布特性”,“网络传递性”(聚类性吧).本文提出网 ...

  2. 封装的一个sorted_vector示例,实现了stl::set的一部分接口

           STL set能保证最坏情况下的查找和插入效率,logN.但是维护红黑树开销较大.set内的元素按照一定的逻辑顺序组织,查找.插入等操作的结果都和排序规则有关.       适合STL ...

  3. 你必须知道的----C语言笔试面试中经典易错的一些知识点(持续更新)

    1. 关于二级指针的解析和引用 1.1  二级指针意义  二级指针存放的是一级指针的地址    Ex: Int a = ; Int *p = &a; Int **q = &p; 1.2 ...

  4. [golang] go的typeswitch guard(类型区别)语法和type assertion(类型断言)语法

    最近在实现golang,看到个go的特性语法: typeswitch guard. typeswitch guard语法如下: package main import "fmt" ...

  5. 《Andrew Ng深度学习》笔记5

    深层神经网络 深层神经网络的组成如图,这里主要是深层神经网络符号的定义. 为什么要用深层神经网络,有什么好处?这里主要是分层的思想.在软件工程中,如果问题遇到困难,一般是通过“加多”一层的方法来解决, ...

  6. springboot访问静态资源遇到的坑

    开始是以这种结构进行的,结果页面上一篇红,访问的页面是这样的 最终找出来问题,虽然每次调整路径都不对,最终查看多种方法可以看到了: 增加: package com.example.demo.confi ...

  7. centos上安装theano和Lasagne

    1.安装theano所需的包 sudo yum install python-devel python-nose python-setuptools gcc gcc-gfortran gcc-c++ ...

  8. RPC 定义 和 原理

    一.RPC 1. RPC是什么 RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议. ...

  9. 详解SimpleXML添加_修改_删除_遍历XML节点属性

    SimpleXML概述 要处理XML 文件,有两种传统的处理思路:SAX 和DOM.SAX 基于事件触发机制,对XML 文件进行一次扫描,完成要进行的处理:DOM 则将整个XML 文件构造为一棵DOM ...

  10. hdu 2654 Be a hero

    ()Become A Hero Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...