I am using below class to do the Deep cloning with out serialization.
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;
}
}
I am inheriting all the classes from this which needs to be cloned.
This works fine with all the objects except key Value Pairs or collections like SortedList,Dictionary etc
Could anyone suggest a way to clone the KeyValue pairs like SortedList of Dictionary.
Aucun commentaire:
Enregistrer un commentaire