日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

返回对应对象的克隆方法

發(fā)布時(shí)間:2023/12/13 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 返回对应对象的克隆方法 小編覺(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
????}
}

?

轉(zhuǎn)載于:https://www.cnblogs.com/magic_evan/archive/2010/04/22/1718226.html

總結(jié)

以上是生活随笔為你收集整理的返回对应对象的克隆方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。