|
Posted on
2009-12-22 13:24
懒人ABC
阅读( 550)
评论()
编辑
收藏
举报
-
-
-
-
-
-
- public abstract class BaseObject : ICloneable
- {
-
-
-
-
- public object Clone()
- {
-
- object newObject = Activator.CreateInstance(this.GetType());
-
- FieldInfo[] fields = newObject.GetType().GetFields();
- int i = 0;
- foreach (FieldInfo fi in this.GetType().GetFields())
- {
-
- Type ICloneType = fi.FieldType.GetInterface("ICloneable", true);
- if (ICloneType != null)
- {
-
- ICloneable IClone = (ICloneable)fi.GetValue(this);
-
- fields[i].SetValue(newObject, IClone.Clone());
- }
- else
- {
-
- fields[i].SetValue(newObject, fi.GetValue(this));
- }
-
-
- Type IEnumerableType = fi.FieldType.GetInterface("IEnumerable", true);
- if (IEnumerableType != null)
- {
-
- IEnumerable IEnum = (IEnumerable)fi.GetValue(this);
- Type IListType = fields[i].FieldType.GetInterface("IList", true);
- Type IDicType = fields[i].FieldType.GetInterface("IDictionary", true);
- int j = 0;
- if (IListType != null)
- {
-
- IList list = (IList)fields[i].GetValue(newObject);
- foreach (object obj in IEnum)
- {
-
- ICloneType = obj.GetType().GetInterface("ICloneable", true);
- if (ICloneType != null)
- {
-
-
- ICloneable clone = (ICloneable)obj;
- list[j] = clone.Clone();
- }
-
-
-
- j++;
- }
- }
- else if (IDicType != null)
- {
-
- IDictionary dic = (IDictionary)fields[i].GetValue(newObject);
- j = 0;
- foreach (DictionaryEntry de in IEnum)
- {
-
- ICloneType = de.Value.GetType().
- GetInterface("ICloneable", true);
- if (ICloneType != null)
- {
- ICloneable clone = (ICloneable)de.Value;
- dic[de.Key] = clone.Clone();
- }
- j++;
- }
- }
- }
- i++;
- }
- return newObject;
- }
- }
|