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 ...
随机推荐
- loj#6041. 「雅礼集训 2017 Day7」事情的相似度(后缀自动机+启发式合并)
题面 传送门 题解 为什么成天有人想搞些大新闻 这里写的是\(yyb\)巨巨说的启发式合并的做法(虽然\(LCT\)的做法不知道比它快到哪里去了--) 建出\(SAM\),那么两个前缀的最长公共后缀就 ...
- equals hashcode toString 方法的使用
package com.wu.toString; import java.util.Date; import java.util.GregorianCalendar; /** * * @author ...
- oracle导出clob 工具
oracle导出clob 等大字段,用pl/sql 等无法导出,除了备份数据库,但是一般数据库中正式环境数据和开发环境中数据不一定完全一致或者合适导出,即使仅仅导出导入一个表,有时候也不方便或者业务上 ...
- PHP编程入门与应用
基础语法.流程控制语句.数组的应用.字符串的应用————3天 PHP函数————3天 面向对象————3天 文件处理————2天 获取页面数据.会话处理————3天 数据库————5天 XML和JSO ...
- npm的介绍
npm使JavaScript开发人员能够轻松地共享和重用代码,并且可以轻松更新你正在共享的代码. 如果你一直在使用JavaScript,你可能已经听说过npm.npm使JavaScript开发人员能够 ...
- 江西理工大学南昌校区排名赛 C: 单身狗的地图游戏
题目描述 萌樱花是一只单身狗. 萌樱花今天决定去喜欢的学妹家玩,但他不记得路,于是他拿出有n个点的完全图,选取了k条不同的路. 完全图:n 个点的图中任意两个点之间都有一条边相连,所以有 n*(n-1 ...
- 不要重复发明轮子-C++STL
闫常友 著. 中国铁道出版社. 2013/5 标准的C++模板库,是算法和其他一些标准组件的集合. . 1.类模板简介 2.c++中的字符串 3.容器 4.c++中的算法 5.迭代器 6.STL 数值 ...
- 1149 Dangerous Goods Packaging (25 分)
When shipping goods with containers, we have to be careful not to pack some incompatible goods into ...
- 从源码角度深入分析 ant
[转自] http://www.tuicool.com/articles/eQvIRbA Ant的基本概念 首先是ant的基本概念: Project,Target,Tasks,Properties,P ...
- poj3187
一.题意:给定n,求1~n的一个排列,这个排列需要满足以下两个要求:1.杨辉三角最后的和为sum 2.字典序最小 二.思路:暴力枚举每一个排列,然后计算和并与sum进行比较.这里我比较费解的是为什么 ...