Base class for cloning an object in C#
Base class for cloning an object in C#
- /// <summary>
- /// BaseObject class is an abstract class for you to derive from.
- /// Every class that will be dirived from this class will support the
- /// Clone method automaticly.<br>
- /// The class implements the interface ICloneable and there
- /// for every object that will be derived <br>
- /// from this object will support the ICloneable interface as well.
- /// </summary>
- public abstract class BaseObject : ICloneable
- {
- /// <summary>
- /// Clone the object, and returning a reference to a cloned object.
- /// </summary>
- /// <returns>Reference to the new cloned
- /// object.</returns>
- public object Clone()
- {
- //First we create an instance of this specific type.
- object newObject = Activator.CreateInstance( this.GetType() );
- //We get the array of fields for the new type instance.
- FieldInfo[] fields = newObject.GetType().GetFields();
- int i = ;
- foreach( FieldInfo fi in this.GetType().GetFields() )
- {
- //We query if the fiels support the ICloneable interface.
- Type ICloneType = fi.FieldType.
- GetInterface( "ICloneable" , true );
- if( ICloneType != null )
- {
- //Getting the ICloneable interface from the object.
- ICloneable IClone = (ICloneable)fi.GetValue(this);
- //We use the clone method to set the new value to the field.
- fields[i].SetValue( newObject , IClone.Clone() );
- }
- else
- {
- // If the field doesn't support the ICloneable
- // interface then just set it.
- fields[i].SetValue( newObject , fi.GetValue(this) );
- }
- //Now we check if the object support the
- //IEnumerable interface, so if it does
- //we need to enumerate all its items and check if
- //they support the ICloneable interface.
- Type IEnumerableType = fi.FieldType.GetInterface
- ( "IEnumerable" , true );
- if( IEnumerableType != null )
- {
- //Get the IEnumerable interface from the field.
- IEnumerable IEnum = (IEnumerable)fi.GetValue(this);
- //This version support the IList and the
- //IDictionary interfaces to iterate on collections.
- Type IListType = fields[i].FieldType.GetInterface
- ( "IList" , true );
- Type IDicType = fields[i].FieldType.GetInterface
- ( "IDictionary" , true );
- int j = ;
- if( IListType != null )
- {
- //Getting the IList interface.
- IList list = (IList)fields[i].GetValue(newObject);
- foreach( object obj in IEnum )
- {
- //Checking to see if the current item
- //support the ICloneable interface.
- ICloneType = obj.GetType().
- GetInterface( "ICloneable" , true );
- if( ICloneType != null )
- {
- //If it does support the ICloneable interface,
- //we use it to set the clone of
- //the object in the list.
- ICloneable clone = (ICloneable)obj;
- list[j] = clone.Clone();
- }
- //NOTE: If the item in the list is not
- //support the ICloneable interface then in the
- //cloned list this item will be the same
- //item as in the original list
- //(as long as this type is a reference type).
- j++;
- }
- }
- else if( IDicType != null )
- {
- //Getting the dictionary interface.
- IDictionary dic = (IDictionary)fields[i].
- GetValue(newObject);
- j = ;
- foreach( DictionaryEntry de in IEnum )
- {
- //Checking to see if the item
- //support the ICloneable interface.
- ICloneType = de.Value.GetType().
- GetInterface( "ICloneable" , true );
- if( ICloneType != null )
- {
- ICloneable clone = (ICloneable)de.Value;
- dic[de.Key] = clone.Clone();
- }
- j++;
- }
- }
- }
- i++;
- }
- return newObject;
- }
- }
Base class for cloning an object in C#的更多相关文章
- js原型链接(二)和object类的create方法
原型链的内部执行方式 <script> function Myclass(){ this.x=" x in Myclass"; } var obj=new Myclas ...
- js Object.create 初探
1.作用 Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__. https://developer.mozilla.org/zh-CN/docs/W ...
- [转]javascript之Object.assign()痛点
本文转自:http://blog.csdn.net/waiterwaiter/article/details/50267787 最近也一直会用javascript,然后中间使用的一些组件,如Echar ...
- ECMAScript5之Object学习笔记(三)
第三部分继续... Object.getOwnPropertyDescriptor(obj, prop) 获取一个对象的属性描述符 根据"Own"这个词我们可以猜到,prop只能是 ...
- 【javascript】base.js
作为一个好的脚手架使用 /* Base.js, version 1.1a Copyright 2006-2010, Dean Edwards License: http://www.opensourc ...
- [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 ...
- c++ object model
对一个结构体进行不断的封装后可以形成一个c++类,为此需要添加很多函数成员之类的代码,为此显示c++比c语言显得庞大并且迟缓,但是事实并不是这些 c++在布局和时间上的额外承担主要是由virtual引 ...
- jquery-1.11.1.js
每次想要使用这个js时,总是要到官网上下载,太麻烦,现在把它收录了 jquery-1.11.1.js /*! * jQuery JavaScript Library v1.11.1 * http ...
- 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 ...
随机推荐
- GN算法---《Community structure in social and biological networks》这篇论文讲了什么?
用中文记下这篇论文的大致意思,以防止忘了.好记性不如烂笔头! 摘要:最近的一些研究在研究社交网络或WWW.研究者都集中于研究网络的“小世界性”,“幂率分布特性”,“网络传递性”(聚类性吧).本文提出网 ...
- 封装的一个sorted_vector示例,实现了stl::set的一部分接口
STL set能保证最坏情况下的查找和插入效率,logN.但是维护红黑树开销较大.set内的元素按照一定的逻辑顺序组织,查找.插入等操作的结果都和排序规则有关. 适合STL ...
- 你必须知道的----C语言笔试面试中经典易错的一些知识点(持续更新)
1. 关于二级指针的解析和引用 1.1 二级指针意义 二级指针存放的是一级指针的地址 Ex: Int a = ; Int *p = &a; Int **q = &p; 1.2 ...
- [golang] go的typeswitch guard(类型区别)语法和type assertion(类型断言)语法
最近在实现golang,看到个go的特性语法: typeswitch guard. typeswitch guard语法如下: package main import "fmt" ...
- 《Andrew Ng深度学习》笔记5
深层神经网络 深层神经网络的组成如图,这里主要是深层神经网络符号的定义. 为什么要用深层神经网络,有什么好处?这里主要是分层的思想.在软件工程中,如果问题遇到困难,一般是通过“加多”一层的方法来解决, ...
- springboot访问静态资源遇到的坑
开始是以这种结构进行的,结果页面上一篇红,访问的页面是这样的 最终找出来问题,虽然每次调整路径都不对,最终查看多种方法可以看到了: 增加: package com.example.demo.confi ...
- centos上安装theano和Lasagne
1.安装theano所需的包 sudo yum install python-devel python-nose python-setuptools gcc gcc-gfortran gcc-c++ ...
- RPC 定义 和 原理
一.RPC 1. RPC是什么 RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议. ...
- 详解SimpleXML添加_修改_删除_遍历XML节点属性
SimpleXML概述 要处理XML 文件,有两种传统的处理思路:SAX 和DOM.SAX 基于事件触发机制,对XML 文件进行一次扫描,完成要进行的处理:DOM 则将整个XML 文件构造为一棵DOM ...
- hdu 2654 Be a hero
()Become A Hero Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...