写程序,用反射深度复制只有公用成员变量(public field)的类(其中类成员仍旧是只有公用成员变量的类)例如public class ABC { public int ID; public string Name; public ABC Child; } 该如何实现啊?
解决方案 »
- this.invoke 未处理, nullReferenceException
- DataGridViewComboBoxColumn怎么结束编辑
- C# 同时POST 文件和参数的难题!
- socket通信中 Dim ServerEP As New IPEndPoint(MyIP, 2001) 其中myip是client的ip 还是service的ip?
- 高手救小弟一命吧,要不我就一命呜呼了!
- 正则表达式的问题,可难住我了
- 如何取得datatable中X字段的不重复值?请进来看详细解释
- C#处理C++ dll 函数
- 谁能给提供一个拨号器的代码!ADSL的
- 如何在现有的图片资源文件中添加图片!这个问题我再加96分(只有96分了)!
- 【求助!】如何双击dataGridView中某空格,复制里面的内容【在线结贴】
- C#递归疑问
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;namespace CopyObject
{
public class CopyObject
{
public virtual object Copy(object SourceObject)
{
if (SourceObject == null)
{
return null;
}
_sourceType = SourceObject.GetType();
_sourceObject = SourceObject; if (_sourceType.IsValueType)
{
_result = SourceObject;
}
else
{
_result = CreateInstance();
}
if (!IsOriginalType())//如果不是c#内置数据类型,则对其进行递归复制
{
CopyProperties();
CopyFields();
}
return _result;
} protected virtual bool IsOriginalType()
{//判断_sourceType是否是c#内置类型,没写完
if (_sourceType == typeof(System.Int32))
{
return true;
}
if (_sourceType == typeof(System.String))
{
return true;
}
if (_sourceType.BaseType == typeof(Enum))
{
return true;
}
return false;
}
protected virtual void CopyFields()
{
FieldInfo[] Fields = _sourceType.GetFields();
foreach (FieldInfo Field in Fields)
{
Field.SetValue(_result, (new CopyObject()).Copy(Field.GetValue(_sourceObject)));
//因为字段的类型可能也是一个类,即需要深度复制,所以递归调用
}
} protected virtual void CopyProperties()
{
PropertyInfo[] Propertyies = _sourceType.GetProperties();
foreach (PropertyInfo Property in Propertyies)
{
Property.SetValue(_result, (new CopyObject()).Copy(Property.GetValue(_sourceObject, null)), null);//对属性的取值和赋值没考虑属性有index的情况,同时因为属性可能是一个类,所以递归调用
}
} protected virtual object CreateInstance()
{
ConstructorInfo Constructor;
Constructor = _sourceType.GetConstructor(new Type[0]);
if (Constructor == null)
{
throw (new Exception("没有找到源对象的无参构造函数"));
}
return Constructor.Invoke(new Type[0]);
}
private object _result, _sourceObject;
private System.Type _sourceType;
}
}
简单的测试代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using CopyObject;namespace TestCopyObject
{
public class LinkList
{
public LinkList()
{
Data = null;
Next = null;
}
public enum even{even, uneven};
public even IsEven;
public object Data;
public LinkList Next;
}
class Program
{
static void Main(string[] args)
{
LinkList oCopyTest = new LinkList(), tmp, Tail, Result;
Tail = oCopyTest;
for (int i = 0; i < 10; ++i)
{
tmp = new LinkList();
tmp.IsEven = ((i % 2 == 0) ? LinkList.even.even : LinkList.even.uneven);
tmp.Data = (object)i;
tmp.Next = null;
Tail.Next = tmp;
Tail = tmp;
}
CopyObject.CopyObject CopyTest = new CopyObject.CopyObject();
Result = (LinkList)CopyTest.Copy(oCopyTest);
Console.WriteLine(Result == oCopyTest);
tmp = Result;
while (tmp != null)
{
Console.WriteLine(tmp.Data);
tmp = tmp.Next;
}
Console.ReadLine();
}
}
}