0
我正在使用下面的類來進行深度克隆而無序列化。使用反射對集合(鍵/值對)進行深度克隆
public class AbstractClone
{
public AbstractClone Clone()
{
Type typeSource = this.GetType();
AbstractClone tObject = (AbstractClone)FormatterServices.GetUninitializedObject(typeSource);
PropertyInfo[] propertyInfo = typeSource.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach (PropertyInfo property in propertyInfo)
{
if (property.CanWrite)
{
if (property.PropertyType.IsValueType || property.PropertyType.IsEnum || property.PropertyType.Equals(typeof(System.String)))
{
property.SetValue(tObject, property.GetValue(this, null), null);
}
else
{
object objPropertyValue = property.GetValue(this, null);
if (objPropertyValue == null)
{
property.SetValue(tObject, null, null);
}
else
{
property.SetValue(tObject, ((AbstractClone)objPropertyValue).Clone(), null);
}
}
}
}
return tObject;
}
}
我繼承了所有需要克隆的類。
這正常工作與所有對象除了鍵值對或集合類似排序列表,字典等
任何人都可以提出一個方法克隆鍵值對像字典的排序列表。