返回对应对象的克隆方法
生活随笔
收集整理的這篇文章主要介紹了
返回对应对象的克隆方法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
代碼 using?System;
using?System.Collections.Generic;
using?System.Text;
using?System.Collections;
using?System.Reflection;
namespace?Common.CloneObjBase
{
????///?<summary>???
????///?BaseObject類是一個(gè)用來(lái)繼承的抽象類。????
????///?每一個(gè)由此類繼承而來(lái)的類將自動(dòng)支持克隆方法。???
????///?該類實(shí)現(xiàn)了Icloneable接口,并且每個(gè)從該對(duì)象繼承而來(lái)的對(duì)象都將同樣地???
????///?支持Icloneable接口。????
????///?</summary>????
????public?abstract?class?CloneObj?:?ICloneable
????{
????????///?<summary>
????????///?Author:Evan?Lee
????????///?QQ:278631309
????????///?Blog:http://www.cnblogs.com/magic_evan
????????///?Email:Can't?be?Find
????????///?</summary>
????????///?<typeparam?name="T">要返回的克隆對(duì)象;該對(duì)象須為class</typeparam>
????????///?<param?name="Tobj">要進(jìn)行克隆的對(duì)象實(shí)例</param>
????????///?<returns></returns>
????????public?T?Clone<T>(T?Tobj)?where?T?:?class
????????{
????????????//首先我們建立指定類型的一個(gè)實(shí)例????????????
????????????object?newObject?=?Activator.CreateInstance(typeof(T));
????????????//我們?nèi)〉眯碌念愋蛯?shí)例的字段數(shù)組。????????????
????????????FieldInfo[]?fields?=?newObject.GetType().GetFields();
????????????int?i?=?0;
????????????foreach?(FieldInfo?fi?in?typeof(T).GetFields())
????????????{
????????????????//我們判斷字段是否支持ICloneable接口。????????????????
????????????????Type?ICloneType?=?fi.FieldType.GetInterface("ICloneable",?true);
????????????????if?(ICloneType?!=?null)
????????????????{
????????????????????//取得對(duì)象的Icloneable接口。????????????????????
????????????????????ICloneable?IClone?=?(ICloneable)fi.GetValue(Tobj);
????????????????????//我們使用克隆方法給字段設(shè)定新值。???????????????????
????????????????????fields[i].SetValue(newObject,?IClone.Clone());
????????????????}
????????????????else
????????????????{
????????????????????//?如果該字段部支持Icloneable接口,直接設(shè)置即可。????????????????????
????????????????????fields[i].SetValue(newObject,?fi.GetValue(Tobj));
????????????????}
????????????????//現(xiàn)在我們檢查該對(duì)象是否支持IEnumerable接口,如果支持,????????????????
????????????????//我們還需要枚舉其所有項(xiàng)并檢查他們是否支持IList?或?IDictionary?接口。???????????????
????????????????Type?IEnumerableType?=?fi.FieldType.GetInterface("IEnumerable",?true);
????????????????if?(IEnumerableType?!=?null)
????????????????{
????????????????????//取得該字段的IEnumerable接口???????????????????
????????????????????IEnumerable?IEnum?=?(IEnumerable)fi.GetValue(Tobj);
????????????????????Type?IListType?=?fields[i].FieldType.GetInterface("IList",?true);
????????????????????Type?IDicType?=?fields[i].FieldType.GetInterface("IDictionary",?true);
????????????????????int?j?=?0;
????????????????????if?(IListType?!=?null)
????????????????????{
????????????????????????//取得IList接口。????????????????????????
????????????????????????IList?list?=?(IList)fields[i].GetValue(newObject);
????????????????????????foreach?(object?obj?in?IEnum)
????????????????????????{
????????????????????????????//查看當(dāng)前項(xiàng)是否支持支持ICloneable?接口。????????????????????????????
????????????????????????????ICloneType?=?obj.GetType().GetInterface("ICloneable",?true);
????????????????????????????if?(ICloneType?!=?null)
????????????????????????????{
????????????????????????????????//如果支持ICloneable?接口,?????????????
????????????????????????????????//我們用它李設(shè)置列表中的對(duì)象的克隆????????????
????????????????????????????????ICloneable?clone?=?(ICloneable)obj;
????????????????????????????????list[j]?=?clone.Clone();
????????????????????????????}
????????????????????????????//注意:如果列表中的項(xiàng)不支持ICloneable接口,那么?????????????????????????
????????????????????????????//在克隆列表的項(xiàng)將與原列表對(duì)應(yīng)項(xiàng)相同?????????????????????????
????????????????????????????//(只要該類型是引用類型)???????????????????????????
????????????????????????????j++;
????????????????????????}
????????????????????}
????????????????????else?if?(IDicType?!=?null)
????????????????????{
????????????????????????//取得IDictionary?接口???????????????????????
????????????????????????IDictionary?dic?=?(IDictionary)fields[i].GetValue(newObject);
????????????????????????j?=?0;
????????????????????????foreach?(DictionaryEntry?de?in?IEnum)
????????????????????????{
????????????????????????????//查看當(dāng)前項(xiàng)是否支持支持ICloneable?接口。????????????????????????????
????????????????????????????ICloneType?=?de.Value.GetType().
????????????????????????????????GetInterface("ICloneable",?true);
????????????????????????????if?(ICloneType?!=?null)
????????????????????????????{
????????????????????????????????ICloneable?clone?=?(ICloneable)de.Value;
????????????????????????????????dic[de.Key]?=?clone.Clone();
????????????????????????????}
????????????????????????????j++;
????????????????????????}
????????????????????}
????????????????}
????????????????i++;
????????????}
????????????return?newObject?as?T;
????????}
????????#region?ICloneable?成員
????????public?object?Clone()
????????{
????????????return?base.MemberwiseClone();
????????????//throw?new?Exception("The?method?or?operation?is?not?implemented.");
????????}
????????#endregion
????}
}
using?System.Collections.Generic;
using?System.Text;
using?System.Collections;
using?System.Reflection;
namespace?Common.CloneObjBase
{
????///?<summary>???
????///?BaseObject類是一個(gè)用來(lái)繼承的抽象類。????
????///?每一個(gè)由此類繼承而來(lái)的類將自動(dòng)支持克隆方法。???
????///?該類實(shí)現(xiàn)了Icloneable接口,并且每個(gè)從該對(duì)象繼承而來(lái)的對(duì)象都將同樣地???
????///?支持Icloneable接口。????
????///?</summary>????
????public?abstract?class?CloneObj?:?ICloneable
????{
????????///?<summary>
????????///?Author:Evan?Lee
????????///?QQ:278631309
????????///?Blog:http://www.cnblogs.com/magic_evan
????????///?Email:Can't?be?Find
????????///?</summary>
????????///?<typeparam?name="T">要返回的克隆對(duì)象;該對(duì)象須為class</typeparam>
????????///?<param?name="Tobj">要進(jìn)行克隆的對(duì)象實(shí)例</param>
????????///?<returns></returns>
????????public?T?Clone<T>(T?Tobj)?where?T?:?class
????????{
????????????//首先我們建立指定類型的一個(gè)實(shí)例????????????
????????????object?newObject?=?Activator.CreateInstance(typeof(T));
????????????//我們?nèi)〉眯碌念愋蛯?shí)例的字段數(shù)組。????????????
????????????FieldInfo[]?fields?=?newObject.GetType().GetFields();
????????????int?i?=?0;
????????????foreach?(FieldInfo?fi?in?typeof(T).GetFields())
????????????{
????????????????//我們判斷字段是否支持ICloneable接口。????????????????
????????????????Type?ICloneType?=?fi.FieldType.GetInterface("ICloneable",?true);
????????????????if?(ICloneType?!=?null)
????????????????{
????????????????????//取得對(duì)象的Icloneable接口。????????????????????
????????????????????ICloneable?IClone?=?(ICloneable)fi.GetValue(Tobj);
????????????????????//我們使用克隆方法給字段設(shè)定新值。???????????????????
????????????????????fields[i].SetValue(newObject,?IClone.Clone());
????????????????}
????????????????else
????????????????{
????????????????????//?如果該字段部支持Icloneable接口,直接設(shè)置即可。????????????????????
????????????????????fields[i].SetValue(newObject,?fi.GetValue(Tobj));
????????????????}
????????????????//現(xiàn)在我們檢查該對(duì)象是否支持IEnumerable接口,如果支持,????????????????
????????????????//我們還需要枚舉其所有項(xiàng)并檢查他們是否支持IList?或?IDictionary?接口。???????????????
????????????????Type?IEnumerableType?=?fi.FieldType.GetInterface("IEnumerable",?true);
????????????????if?(IEnumerableType?!=?null)
????????????????{
????????????????????//取得該字段的IEnumerable接口???????????????????
????????????????????IEnumerable?IEnum?=?(IEnumerable)fi.GetValue(Tobj);
????????????????????Type?IListType?=?fields[i].FieldType.GetInterface("IList",?true);
????????????????????Type?IDicType?=?fields[i].FieldType.GetInterface("IDictionary",?true);
????????????????????int?j?=?0;
????????????????????if?(IListType?!=?null)
????????????????????{
????????????????????????//取得IList接口。????????????????????????
????????????????????????IList?list?=?(IList)fields[i].GetValue(newObject);
????????????????????????foreach?(object?obj?in?IEnum)
????????????????????????{
????????????????????????????//查看當(dāng)前項(xiàng)是否支持支持ICloneable?接口。????????????????????????????
????????????????????????????ICloneType?=?obj.GetType().GetInterface("ICloneable",?true);
????????????????????????????if?(ICloneType?!=?null)
????????????????????????????{
????????????????????????????????//如果支持ICloneable?接口,?????????????
????????????????????????????????//我們用它李設(shè)置列表中的對(duì)象的克隆????????????
????????????????????????????????ICloneable?clone?=?(ICloneable)obj;
????????????????????????????????list[j]?=?clone.Clone();
????????????????????????????}
????????????????????????????//注意:如果列表中的項(xiàng)不支持ICloneable接口,那么?????????????????????????
????????????????????????????//在克隆列表的項(xiàng)將與原列表對(duì)應(yīng)項(xiàng)相同?????????????????????????
????????????????????????????//(只要該類型是引用類型)???????????????????????????
????????????????????????????j++;
????????????????????????}
????????????????????}
????????????????????else?if?(IDicType?!=?null)
????????????????????{
????????????????????????//取得IDictionary?接口???????????????????????
????????????????????????IDictionary?dic?=?(IDictionary)fields[i].GetValue(newObject);
????????????????????????j?=?0;
????????????????????????foreach?(DictionaryEntry?de?in?IEnum)
????????????????????????{
????????????????????????????//查看當(dāng)前項(xiàng)是否支持支持ICloneable?接口。????????????????????????????
????????????????????????????ICloneType?=?de.Value.GetType().
????????????????????????????????GetInterface("ICloneable",?true);
????????????????????????????if?(ICloneType?!=?null)
????????????????????????????{
????????????????????????????????ICloneable?clone?=?(ICloneable)de.Value;
????????????????????????????????dic[de.Key]?=?clone.Clone();
????????????????????????????}
????????????????????????????j++;
????????????????????????}
????????????????????}
????????????????}
????????????????i++;
????????????}
????????????return?newObject?as?T;
????????}
????????#region?ICloneable?成員
????????public?object?Clone()
????????{
????????????return?base.MemberwiseClone();
????????????//throw?new?Exception("The?method?or?operation?is?not?implemented.");
????????}
????????#endregion
????}
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/magic_evan/archive/2010/04/22/1718226.html
總結(jié)
以上是生活随笔為你收集整理的返回对应对象的克隆方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 提交按钮禁用的办法
- 下一篇: 控件(View)之TextSwitche