Reflection-based cloner now also clones arrays (including deep clones of array values if desired)
git-svn-id: file:///srv/devel/repo-conversion/nusu@225 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
bc8e565f8f
commit
7f7b9eff19
|
@ -76,6 +76,10 @@ namespace Nuclex.Support.Cloning {
|
||||||
public TestReferenceType ReferenceTypeField;
|
public TestReferenceType ReferenceTypeField;
|
||||||
/// <summary>Reference type property for testing</summary>
|
/// <summary>Reference type property for testing</summary>
|
||||||
public TestReferenceType ReferenceTypeProperty { get; set; }
|
public TestReferenceType ReferenceTypeProperty { get; set; }
|
||||||
|
/// <summary>An array field of reference types</summary>
|
||||||
|
public TestReferenceType[,][] ReferenceTypeArrayField;
|
||||||
|
/// <summary>An array property of reference types</summary>
|
||||||
|
public TestReferenceType[,][] ReferenceTypeArrayProperty { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,6 +102,10 @@ namespace Nuclex.Support.Cloning {
|
||||||
public TestReferenceType ReferenceTypeField;
|
public TestReferenceType ReferenceTypeField;
|
||||||
/// <summary>Reference type property for testing</summary>
|
/// <summary>Reference type property for testing</summary>
|
||||||
public TestReferenceType ReferenceTypeProperty { get; set; }
|
public TestReferenceType ReferenceTypeProperty { get; set; }
|
||||||
|
/// <summary>An array field of reference types</summary>
|
||||||
|
public TestReferenceType[,][] ReferenceTypeArrayField;
|
||||||
|
/// <summary>An array property of reference types</summary>
|
||||||
|
public TestReferenceType[,][] ReferenceTypeArrayProperty { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -399,6 +407,32 @@ namespace Nuclex.Support.Cloning {
|
||||||
return new HierarchicalValueType() {
|
return new HierarchicalValueType() {
|
||||||
TestField = 123,
|
TestField = 123,
|
||||||
TestProperty = 321,
|
TestProperty = 321,
|
||||||
|
ReferenceTypeArrayField = new TestReferenceType[2, 4][] {
|
||||||
|
{
|
||||||
|
null, null, null, null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
null, null, null,
|
||||||
|
new TestReferenceType[3] {
|
||||||
|
new TestReferenceType() { TestField = 101, TestProperty = 202 },
|
||||||
|
null,
|
||||||
|
new TestReferenceType() { TestField = 909, TestProperty = 808 }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ReferenceTypeArrayProperty = new TestReferenceType[2, 4][] {
|
||||||
|
{
|
||||||
|
null, null, null, null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
null, null, null,
|
||||||
|
new TestReferenceType[3] {
|
||||||
|
new TestReferenceType() { TestField = 303, TestProperty = 404 },
|
||||||
|
null,
|
||||||
|
new TestReferenceType() { TestField = 707, TestProperty = 606 }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
ValueTypeField = new TestValueType() {
|
ValueTypeField = new TestValueType() {
|
||||||
TestField = 456,
|
TestField = 456,
|
||||||
TestProperty = 654
|
TestProperty = 654
|
||||||
|
@ -424,6 +458,32 @@ namespace Nuclex.Support.Cloning {
|
||||||
return new HierarchicalReferenceType() {
|
return new HierarchicalReferenceType() {
|
||||||
TestField = 123,
|
TestField = 123,
|
||||||
TestProperty = 321,
|
TestProperty = 321,
|
||||||
|
ReferenceTypeArrayField = new TestReferenceType[2, 4][] {
|
||||||
|
{
|
||||||
|
null, null, null, null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
null, null, null,
|
||||||
|
new TestReferenceType[3] {
|
||||||
|
new TestReferenceType() { TestField = 101, TestProperty = 202 },
|
||||||
|
null,
|
||||||
|
new TestReferenceType() { TestField = 909, TestProperty = 808 }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ReferenceTypeArrayProperty = new TestReferenceType[2, 4][] {
|
||||||
|
{
|
||||||
|
null, null, null, null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
null, null, null,
|
||||||
|
new TestReferenceType[3] {
|
||||||
|
new TestReferenceType() { TestField = 303, TestProperty = 404 },
|
||||||
|
null,
|
||||||
|
new TestReferenceType() { TestField = 707, TestProperty = 606 }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
ValueTypeField = new TestValueType() {
|
ValueTypeField = new TestValueType() {
|
||||||
TestField = 456,
|
TestField = 456,
|
||||||
TestProperty = 654
|
TestProperty = 654
|
||||||
|
|
|
@ -23,10 +23,8 @@ using System.Reflection;
|
||||||
|
|
||||||
namespace Nuclex.Support.Cloning {
|
namespace Nuclex.Support.Cloning {
|
||||||
|
|
||||||
// TODO: Doesn't clone arrays yet
|
|
||||||
|
|
||||||
/// <summary>Clones objects using reflection</summary>
|
/// <summary>Clones objects using reflection</summary>
|
||||||
public class ReflectionCloner : ICloneFactory, IStateCopier {
|
public class ReflectionCloner : ICloneFactory {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a shallow clone of the specified object, reusing any referenced objects
|
/// Creates a shallow clone of the specified object, reusing any referenced objects
|
||||||
|
@ -38,22 +36,23 @@ namespace Nuclex.Support.Cloning {
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <returns>A shallow clone of the provided object</returns>
|
/// <returns>A shallow clone of the provided object</returns>
|
||||||
public TCloned ShallowClone<TCloned>(TCloned objectToClone, bool usePropertyBasedClone) {
|
public TCloned ShallowClone<TCloned>(TCloned objectToClone, bool usePropertyBasedClone) {
|
||||||
if(typeof(TCloned).IsValueType) {
|
Type originalType = objectToClone.GetType();
|
||||||
TCloned clone = Activator.CreateInstance<TCloned>();
|
if(originalType.IsPrimitive) {
|
||||||
|
return objectToClone; // Being value types, primitives are copied by default
|
||||||
|
} else if(originalType.IsArray) {
|
||||||
|
return (TCloned)shallowCloneArray(objectToClone);
|
||||||
|
} else if(originalType.IsValueType) {
|
||||||
if(usePropertyBasedClone) {
|
if(usePropertyBasedClone) {
|
||||||
shallowCopyValueTypePropertyBased<TCloned>(ref objectToClone, ref clone);
|
return (TCloned)shallowCloneComplexPropertyBased(objectToClone);
|
||||||
} else {
|
} else {
|
||||||
shallowCopyValueTypeFieldBased<TCloned>(ref objectToClone, ref clone);
|
return objectToClone; // Value types can be copied directly
|
||||||
}
|
}
|
||||||
return clone;
|
|
||||||
} else {
|
} else {
|
||||||
TCloned clone = (TCloned)Activator.CreateInstance(objectToClone.GetType());
|
|
||||||
if(usePropertyBasedClone) {
|
if(usePropertyBasedClone) {
|
||||||
shallowCopyReferenceTypePropertyBased(objectToClone.GetType(), objectToClone, clone);
|
return (TCloned)shallowCloneComplexPropertyBased(objectToClone);
|
||||||
} else {
|
} else {
|
||||||
shallowCopyReferenceTypeFieldBased(objectToClone.GetType(), objectToClone, clone);
|
return (TCloned)shallowCloneComplexFieldBased(objectToClone);
|
||||||
}
|
}
|
||||||
return clone;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,360 +67,296 @@ namespace Nuclex.Support.Cloning {
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <returns>A deep clone of the provided object</returns>
|
/// <returns>A deep clone of the provided object</returns>
|
||||||
public TCloned DeepClone<TCloned>(TCloned objectToClone, bool usePropertyBasedClone) {
|
public TCloned DeepClone<TCloned>(TCloned objectToClone, bool usePropertyBasedClone) {
|
||||||
if(typeof(TCloned).IsValueType) {
|
|
||||||
TCloned clone = Activator.CreateInstance<TCloned>();
|
|
||||||
if(usePropertyBasedClone) {
|
if(usePropertyBasedClone) {
|
||||||
deepCopyValueTypePropertyBased<TCloned>(ref objectToClone, ref clone);
|
return (TCloned)deepCloneSinglePropertyBased(objectToClone);
|
||||||
} else {
|
} else {
|
||||||
deepCopyValueTypeFieldBased<TCloned>(ref objectToClone, ref clone);
|
return (TCloned)deepCloneSingleFieldBased(objectToClone);
|
||||||
}
|
|
||||||
return clone;
|
|
||||||
} else {
|
|
||||||
TCloned clone = (TCloned)Activator.CreateInstance(objectToClone.GetType());
|
|
||||||
if(usePropertyBasedClone) {
|
|
||||||
deepCopyReferenceTypePropertyBased(objectToClone.GetType(), objectToClone, clone);
|
|
||||||
} else {
|
|
||||||
deepCopyReferenceTypeFieldBased(objectToClone.GetType(), objectToClone, clone);
|
|
||||||
}
|
|
||||||
return clone;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Transfers the state of one object into another</summary>
|
/// <summary>Clones a complex type using field-based value transfer</summary>
|
||||||
/// <typeparam name="TState">Type of the object whose sate will be transferred</typeparam>
|
/// <param name="original">Original instance that will be cloned</param>
|
||||||
/// <param name="original">Original instance the state will be taken from</param>
|
/// <returns>A clone of the original instance</returns>
|
||||||
/// <param name="target">Target instance the state will be written to</param>
|
private object shallowCloneComplexFieldBased(object original) {
|
||||||
/// <param name="propertyBased">Whether to perform a property-based state copy</param>
|
Type originalType = original.GetType();
|
||||||
public void ShallowCopyState<TState>(TState original, TState target, bool propertyBased)
|
object clone = Activator.CreateInstance(originalType);
|
||||||
where TState : class {
|
|
||||||
if(propertyBased) {
|
|
||||||
shallowCopyReferenceTypePropertyBased(typeof(TState), original, target);
|
|
||||||
} else {
|
|
||||||
shallowCopyReferenceTypeFieldBased(typeof(TState), original, target);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Transfers the state of one object into another</summary>
|
FieldInfo[] fieldInfos = originalType.GetFields(
|
||||||
/// <typeparam name="TState">Type of the object whose sate will be transferred</typeparam>
|
|
||||||
/// <param name="original">Original instance the state will be taken from</param>
|
|
||||||
/// <param name="target">Target instance the state will be written to</param>
|
|
||||||
/// <param name="propertyBased">Whether to perform a property-based state copy</param>
|
|
||||||
public void ShallowCopyState<TState>(
|
|
||||||
ref TState original, ref TState target, bool propertyBased
|
|
||||||
) where TState : struct {
|
|
||||||
if(propertyBased) {
|
|
||||||
shallowCopyValueTypePropertyBased<TState>(ref original, ref target);
|
|
||||||
} else {
|
|
||||||
shallowCopyValueTypeFieldBased<TState>(ref original, ref target);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Transfers the state of one object into another, creating clones of referenced objects
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TState">Type of the object whose sate will be transferred</typeparam>
|
|
||||||
/// <param name="original">Original instance the state will be taken from</param>
|
|
||||||
/// <param name="target">Target instance the state will be written to</param>
|
|
||||||
/// <param name="propertyBased">Whether to perform a property-based state copy</param>
|
|
||||||
public void DeepCopyState<TState>(TState original, TState target, bool propertyBased)
|
|
||||||
where TState : class {
|
|
||||||
if(propertyBased) {
|
|
||||||
deepCopyReferenceTypePropertyBased(typeof(TState), original, target);
|
|
||||||
} else {
|
|
||||||
deepCopyReferenceTypeFieldBased(typeof(TState), original, target);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Transfers the state of one object into another, creating clones of referenced objects
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TState">Type of the object whose sate will be transferred</typeparam>
|
|
||||||
/// <param name="original">Original instance the state will be taken from</param>
|
|
||||||
/// <param name="target">Target instance the state will be written to</param>
|
|
||||||
/// <param name="propertyBased">Whether to perform a property-based state copy</param>
|
|
||||||
public void DeepCopyState<TState>(ref TState original, ref TState target, bool propertyBased)
|
|
||||||
where TState : struct {
|
|
||||||
if(propertyBased) {
|
|
||||||
deepCopyValueTypePropertyBased<TState>(ref original, ref target);
|
|
||||||
} else {
|
|
||||||
deepCopyValueTypeFieldBased<TState>(ref original, ref target);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Creates a field-based shallow copy of a reference type</summary>
|
|
||||||
/// <param name="copiedType">Type the copy will be based upon</param>
|
|
||||||
/// <param name="original">Original object that will be copied</param>
|
|
||||||
/// <param name="target">Target object into which copied values will be written</param>
|
|
||||||
private static void shallowCopyReferenceTypeFieldBased(
|
|
||||||
Type copiedType, object original, object target
|
|
||||||
) {
|
|
||||||
FieldInfo[] fieldInfos = copiedType.GetFields(
|
|
||||||
BindingFlags.Public | BindingFlags.NonPublic |
|
BindingFlags.Public | BindingFlags.NonPublic |
|
||||||
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
||||||
);
|
);
|
||||||
for(int index = 0; index < fieldInfos.Length; ++index) {
|
for(int index = 0; index < fieldInfos.Length; ++index) {
|
||||||
FieldInfo fieldInfo = fieldInfos[index];
|
FieldInfo fieldInfo = fieldInfos[index];
|
||||||
fieldInfo.SetValue(target, fieldInfo.GetValue(original));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Creates a property-based shallow copy of a reference type</summary>
|
|
||||||
/// <param name="copiedType">Type the copy will be based upon</param>
|
|
||||||
/// <param name="original">Original object that will be copied</param>
|
|
||||||
/// <param name="target">Target object into which copied values will be written</param>
|
|
||||||
private static void shallowCopyReferenceTypePropertyBased(
|
|
||||||
Type copiedType, object original, object target
|
|
||||||
) {
|
|
||||||
PropertyInfo[] propertyInfos = copiedType.GetProperties(
|
|
||||||
BindingFlags.Public | BindingFlags.NonPublic |
|
|
||||||
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
|
||||||
);
|
|
||||||
for(int index = 0; index < propertyInfos.Length; ++index) {
|
|
||||||
PropertyInfo propertyInfo = propertyInfos[index];
|
|
||||||
if(propertyInfo.CanRead && propertyInfo.CanWrite) {
|
|
||||||
if(propertyInfo.PropertyType.IsPrimitive) {
|
|
||||||
propertyInfo.SetValue(
|
|
||||||
target,
|
|
||||||
propertyInfo.GetValue(original, null),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
} else if(propertyInfo.PropertyType.IsValueType) {
|
|
||||||
// Recurse into the value type - value types are seen as part of
|
|
||||||
// the outer type and a shallow copy does follow their hierarchy.
|
|
||||||
// This is equivalent to what would happen if you directly assign
|
|
||||||
// one value type to another.
|
|
||||||
object boxedOriginalValue = propertyInfo.GetValue(original, null);
|
|
||||||
Type originalType = boxedOriginalValue.GetType();
|
|
||||||
object boxedClonedValue = Activator.CreateInstance(originalType);
|
|
||||||
shallowCopyReferenceTypePropertyBased(
|
|
||||||
originalType, boxedOriginalValue, boxedClonedValue
|
|
||||||
);
|
|
||||||
propertyInfo.SetValue(target, boxedClonedValue, null);
|
|
||||||
} else {
|
|
||||||
propertyInfo.SetValue(
|
|
||||||
target,
|
|
||||||
propertyInfo.GetValue(original, null),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Creates a property-based shallow copy of a value type</summary>
|
|
||||||
/// <typeparam name="TState">Value type that will be copied</typeparam>
|
|
||||||
/// <param name="original">Original object that will be copied</param>
|
|
||||||
/// <param name="target">Target object into which copied values will be written</param>
|
|
||||||
private static void shallowCopyValueTypePropertyBased<TState>(
|
|
||||||
ref TState original, ref TState target
|
|
||||||
) {
|
|
||||||
object boxedOriginal = original;
|
|
||||||
object boxedTarget = target;
|
|
||||||
|
|
||||||
PropertyInfo[] propertyInfos = typeof(TState).GetProperties(
|
|
||||||
BindingFlags.Public | BindingFlags.NonPublic |
|
|
||||||
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
|
||||||
);
|
|
||||||
for(int index = 0; index < propertyInfos.Length; ++index) {
|
|
||||||
PropertyInfo propertyInfo = propertyInfos[index];
|
|
||||||
|
|
||||||
if(propertyInfo.CanRead && propertyInfo.CanWrite) {
|
|
||||||
if(propertyInfo.PropertyType.IsPrimitive) {
|
|
||||||
propertyInfo.SetValue(
|
|
||||||
boxedTarget,
|
|
||||||
propertyInfo.GetValue(boxedOriginal, null),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
} else if(propertyInfo.PropertyType.IsValueType) {
|
|
||||||
object boxedOriginalValue = propertyInfo.GetValue(original, null);
|
|
||||||
Type originalType = boxedOriginalValue.GetType();
|
|
||||||
object boxedClonedValue = Activator.CreateInstance(originalType);
|
|
||||||
deepCopyReferenceTypePropertyBased(
|
|
||||||
originalType, boxedOriginalValue, boxedClonedValue
|
|
||||||
);
|
|
||||||
propertyInfo.SetValue(boxedTarget, boxedClonedValue, null);
|
|
||||||
} else {
|
|
||||||
propertyInfo.SetValue(
|
|
||||||
boxedTarget,
|
|
||||||
propertyInfo.GetValue(boxedOriginal, null),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
target = (TState)boxedTarget;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Creates a field-based shallow copy of a value type</summary>
|
|
||||||
/// <typeparam name="TState">Value type that will be copied</typeparam>
|
|
||||||
/// <param name="original">Original object that will be copied</param>
|
|
||||||
/// <param name="target">Target object into which copied values will be written</param>
|
|
||||||
private static void shallowCopyValueTypeFieldBased<TState>(
|
|
||||||
ref TState original, ref TState target
|
|
||||||
) {
|
|
||||||
target = original; // hehe
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Creates a field-based deep copy of a reference type</summary>
|
|
||||||
/// <param name="copiedType">Type the copy will be based upon</param>
|
|
||||||
/// <param name="original">Original object that will be copied</param>
|
|
||||||
/// <param name="target">Target object into which copied values will be written</param>
|
|
||||||
private static void deepCopyReferenceTypeFieldBased(
|
|
||||||
Type copiedType, object original, object target
|
|
||||||
) {
|
|
||||||
FieldInfo[] fieldInfos = copiedType.GetFields(
|
|
||||||
BindingFlags.Public | BindingFlags.NonPublic |
|
|
||||||
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
|
||||||
);
|
|
||||||
for(int index = 0; index < fieldInfos.Length; ++index) {
|
|
||||||
FieldInfo fieldInfo = fieldInfos[index];
|
|
||||||
if(fieldInfo.FieldType.IsPrimitive) {
|
|
||||||
fieldInfo.SetValue(target, fieldInfo.GetValue(original));
|
|
||||||
} else if(fieldInfo.FieldType.IsValueType) {
|
|
||||||
object boxedOriginalValue = fieldInfo.GetValue(original);
|
|
||||||
Type originalType = boxedOriginalValue.GetType();
|
|
||||||
object boxedClonedValue = Activator.CreateInstance(originalType);
|
|
||||||
deepCopyReferenceTypeFieldBased(originalType, boxedOriginalValue, boxedClonedValue);
|
|
||||||
fieldInfo.SetValue(target, boxedClonedValue);
|
|
||||||
} else {
|
|
||||||
object originalValue = fieldInfo.GetValue(original);
|
object originalValue = fieldInfo.GetValue(original);
|
||||||
if(originalValue != null) {
|
if(originalValue != null) {
|
||||||
Type originalType = originalValue.GetType();
|
// Everything's just directly assigned in a shallow clone
|
||||||
object clonedValue = Activator.CreateInstance(originalType);
|
fieldInfo.SetValue(clone, originalValue);
|
||||||
deepCopyReferenceTypeFieldBased(originalType, originalValue, clonedValue);
|
|
||||||
fieldInfo.SetValue(target, clonedValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Creates a property-based deep copy of a reference type</summary>
|
return clone;
|
||||||
/// <param name="copiedType">Type the copy will be based upon</param>
|
}
|
||||||
/// <param name="original">Original object that will be copied</param>
|
|
||||||
/// <param name="target">Target object into which copied values will be written</param>
|
/// <summary>Clones a complex type using property-based value transfer</summary>
|
||||||
private static void deepCopyReferenceTypePropertyBased(
|
/// <param name="original">Original instance that will be cloned</param>
|
||||||
Type copiedType, object original, object target
|
/// <returns>A clone of the original instance</returns>
|
||||||
) {
|
private object shallowCloneComplexPropertyBased(object original) {
|
||||||
PropertyInfo[] propertyInfos = copiedType.GetProperties(
|
Type originalType = original.GetType();
|
||||||
|
object clone = Activator.CreateInstance(originalType);
|
||||||
|
|
||||||
|
PropertyInfo[] propertyInfos = originalType.GetProperties(
|
||||||
BindingFlags.Public | BindingFlags.NonPublic |
|
BindingFlags.Public | BindingFlags.NonPublic |
|
||||||
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
||||||
);
|
);
|
||||||
for(int index = 0; index < propertyInfos.Length; ++index) {
|
for(int index = 0; index < propertyInfos.Length; ++index) {
|
||||||
PropertyInfo propertyInfo = propertyInfos[index];
|
PropertyInfo propertyInfo = propertyInfos[index];
|
||||||
if(propertyInfo.CanRead && propertyInfo.CanWrite) {
|
Type propertyType = propertyInfo.PropertyType;
|
||||||
if(propertyInfo.PropertyType.IsPrimitive) {
|
|
||||||
propertyInfo.SetValue(
|
|
||||||
target,
|
|
||||||
propertyInfo.GetValue(original, null),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
} else if(propertyInfo.PropertyType.IsValueType) {
|
|
||||||
object boxedOriginalValue = propertyInfo.GetValue(original, null);
|
|
||||||
Type originalType = boxedOriginalValue.GetType();
|
|
||||||
object boxedClonedValue = Activator.CreateInstance(originalType);
|
|
||||||
deepCopyReferenceTypePropertyBased(
|
|
||||||
originalType, boxedOriginalValue, boxedClonedValue
|
|
||||||
);
|
|
||||||
propertyInfo.SetValue(target, boxedClonedValue, null);
|
|
||||||
} else {
|
|
||||||
object originalValue = propertyInfo.GetValue(original, null);
|
object originalValue = propertyInfo.GetValue(original, null);
|
||||||
if(originalValue != null) {
|
if(originalValue != null) {
|
||||||
Type originalType = originalValue.GetType();
|
if(propertyType.IsPrimitive) { // Primitive types can be assigned directly
|
||||||
object clonedValue = Activator.CreateInstance(originalType);
|
propertyInfo.SetValue(clone, originalValue, null);
|
||||||
deepCopyReferenceTypePropertyBased(
|
} else if(propertyType.IsValueType) {
|
||||||
originalType, originalValue, clonedValue
|
// Value types are seen as part of the original type and are thus recursed into
|
||||||
);
|
propertyInfo.SetValue(clone, shallowCloneComplexPropertyBased(originalValue), null);
|
||||||
propertyInfo.SetValue(target, clonedValue, null);
|
} else if(propertyType.IsArray) { // Arrays are assigned directly in a shallow clone
|
||||||
}
|
propertyInfo.SetValue(clone, originalValue, null);
|
||||||
}
|
} else { // Complex types are directly assigned without creating a copy
|
||||||
|
propertyInfo.SetValue(clone, originalValue, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Creates a field-based deep copy of a value type</summary>
|
return clone;
|
||||||
/// <typeparam name="TState">Value type that will be copied</typeparam>
|
}
|
||||||
/// <param name="original">Original object that will be copied</param>
|
|
||||||
/// <param name="target">Target object into which copied values will be written</param>
|
|
||||||
private static void deepCopyValueTypeFieldBased<TState>(
|
|
||||||
ref TState original, ref TState target
|
|
||||||
) {
|
|
||||||
object boxedOriginal = original;
|
|
||||||
object boxedTarget = Activator.CreateInstance(original.GetType());
|
|
||||||
|
|
||||||
FieldInfo[] fieldInfos = typeof(TState).GetFields(
|
/// <summary>Clones an array using field-based value transfer</summary>
|
||||||
|
/// <param name="original">Original array that will be cloned</param>
|
||||||
|
/// <returns>A clone of the original array</returns>
|
||||||
|
private object shallowCloneArray(object original) {
|
||||||
|
return ((Array)original).Clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Copies a single object using field-based value transfer</summary>
|
||||||
|
/// <param name="original">Original object that will be cloned</param>
|
||||||
|
/// <returns>A clone of the original object</returns>
|
||||||
|
private static object deepCloneSingleFieldBased(object original) {
|
||||||
|
Type originalType = original.GetType();
|
||||||
|
if(originalType.IsPrimitive) {
|
||||||
|
return original; // Creates another box, does not reference boxed primitive
|
||||||
|
} else if(originalType.IsArray) {
|
||||||
|
return deepCloneArrayFieldBased((Array)original, originalType.GetElementType());
|
||||||
|
} else {
|
||||||
|
return deepCloneComplexFieldBased(original);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Clones a complex type using field-based value transfer</summary>
|
||||||
|
/// <param name="original">Original instance that will be cloned</param>
|
||||||
|
/// <returns>A clone of the original instance</returns>
|
||||||
|
private static object deepCloneComplexFieldBased(object original) {
|
||||||
|
Type originalType = original.GetType();
|
||||||
|
object clone = Activator.CreateInstance(originalType);
|
||||||
|
|
||||||
|
FieldInfo[] fieldInfos = originalType.GetFields(
|
||||||
BindingFlags.Public | BindingFlags.NonPublic |
|
BindingFlags.Public | BindingFlags.NonPublic |
|
||||||
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
||||||
);
|
);
|
||||||
for(int index = 0; index < fieldInfos.Length; ++index) {
|
for(int index = 0; index < fieldInfos.Length; ++index) {
|
||||||
FieldInfo fieldInfo = fieldInfos[index];
|
FieldInfo fieldInfo = fieldInfos[index];
|
||||||
if(fieldInfo.FieldType.IsPrimitive) {
|
Type fieldType = fieldInfo.FieldType;
|
||||||
object boxedValue = fieldInfo.GetValue(boxedOriginal);
|
object originalValue = fieldInfo.GetValue(original);
|
||||||
fieldInfo.SetValue(boxedTarget, boxedValue);
|
|
||||||
} else if(fieldInfo.FieldType.IsValueType) {
|
|
||||||
object boxedOriginalValue = fieldInfo.GetValue(boxedOriginal);
|
|
||||||
Type originalType = boxedOriginalValue.GetType();
|
|
||||||
object boxedClonedValue = Activator.CreateInstance(originalType);
|
|
||||||
deepCopyReferenceTypeFieldBased(originalType, boxedOriginalValue, boxedClonedValue);
|
|
||||||
fieldInfo.SetValue(boxedTarget, boxedClonedValue);
|
|
||||||
} else {
|
|
||||||
object originalValue = fieldInfo.GetValue(boxedOriginal);
|
|
||||||
if(originalValue != null) {
|
if(originalValue != null) {
|
||||||
Type originalType = originalValue.GetType();
|
if(fieldType.IsPrimitive) { // Primitive types can be assigned directly
|
||||||
object clonedValue = Activator.CreateInstance(originalType);
|
fieldInfo.SetValue(clone, originalValue);
|
||||||
deepCopyReferenceTypeFieldBased(originalType, originalValue, clonedValue);
|
} else if(fieldType.IsArray) { // Arrays need to be cloned element-by-element
|
||||||
fieldInfo.SetValue(boxedTarget, clonedValue);
|
fieldInfo.SetValue(
|
||||||
|
clone,
|
||||||
|
deepCloneArrayFieldBased((Array)originalValue, fieldType.GetElementType())
|
||||||
|
);
|
||||||
|
} else { // Complex types need to be cloned member-by-member
|
||||||
|
fieldInfo.SetValue(clone, deepCloneSingleFieldBased(originalValue));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
target = (TState)boxedTarget;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Creates a property-based shallow copy of a value type</summary>
|
/// <summary>Clones an array using field-based value transfer</summary>
|
||||||
/// <typeparam name="TState">Value type that will be copied</typeparam>
|
/// <param name="original">Original array that will be cloned</param>
|
||||||
/// <param name="original">Original object that will be copied</param>
|
/// <param name="elementType">Type of elements the original array contains</param>
|
||||||
/// <param name="target">Target object into which copied values will be written</param>
|
/// <returns>A clone of the original array</returns>
|
||||||
private static void deepCopyValueTypePropertyBased<TState>(
|
private static object deepCloneArrayFieldBased(Array original, Type elementType) {
|
||||||
ref TState original, ref TState target
|
if(elementType.IsPrimitive) {
|
||||||
) {
|
return original.Clone();
|
||||||
object boxedOriginal = original;
|
}
|
||||||
object boxedTarget = Activator.CreateInstance(original.GetType());
|
|
||||||
|
|
||||||
PropertyInfo[] propertyInfos = typeof(TState).GetProperties(
|
int dimensionCount = original.Rank;
|
||||||
|
|
||||||
|
// Find out the length of each of the array's dimensions, also calculate how
|
||||||
|
// many elements there are in the array in total.
|
||||||
|
var lengths = new int[dimensionCount];
|
||||||
|
int totalElementCount = 0;
|
||||||
|
for(int index = 0; index < dimensionCount; ++index) {
|
||||||
|
lengths[index] = original.GetLength(index);
|
||||||
|
if(index == 0) {
|
||||||
|
totalElementCount = lengths[index];
|
||||||
|
} else {
|
||||||
|
totalElementCount *= lengths[index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Knowing the number of dimensions and the length of each dimension, we can
|
||||||
|
// create another array of the exact same sizes.
|
||||||
|
Array clone = Array.CreateInstance(elementType, lengths);
|
||||||
|
|
||||||
|
// If this is a one-dimensional array (most common type), do an optimized copy
|
||||||
|
// directly specifying the indices
|
||||||
|
if(dimensionCount == 1) {
|
||||||
|
|
||||||
|
// Clone each element of the array directly
|
||||||
|
for(int index = 0; index < totalElementCount; ++index) {
|
||||||
|
object originalElement = original.GetValue(index);
|
||||||
|
if(originalElement != null) {
|
||||||
|
clone.SetValue(deepCloneSingleFieldBased(originalElement), index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else { // Otherwise use the generic code for multi-dimensional arrays
|
||||||
|
|
||||||
|
var indices = new int[dimensionCount];
|
||||||
|
for(int index = 0; index < totalElementCount; ++index) {
|
||||||
|
|
||||||
|
// Determine the index for each of the array's dimensions
|
||||||
|
int elementIndex = index;
|
||||||
|
for(int dimensionIndex = dimensionCount - 1; dimensionIndex >= 0; --dimensionIndex) {
|
||||||
|
indices[dimensionIndex] = elementIndex % lengths[dimensionIndex];
|
||||||
|
elementIndex /= lengths[dimensionIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clone the current array element
|
||||||
|
object originalElement = original.GetValue(indices);
|
||||||
|
if(originalElement != null) {
|
||||||
|
clone.SetValue(deepCloneSingleFieldBased(originalElement), indices);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Copies a single object using property-based value transfer</summary>
|
||||||
|
/// <param name="original">Original object that will be cloned</param>
|
||||||
|
/// <returns>A clone of the original object</returns>
|
||||||
|
private static object deepCloneSinglePropertyBased(object original) {
|
||||||
|
Type originalType = original.GetType();
|
||||||
|
if(originalType.IsPrimitive) {
|
||||||
|
return original; // Creates another box, does not reference boxed primitive
|
||||||
|
} else if(originalType.IsArray) {
|
||||||
|
return deepCloneArrayPropertyBased((Array)original, originalType.GetElementType());
|
||||||
|
} else {
|
||||||
|
return deepCloneComplexPropertyBased(original);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Clones a complex type using property-based value transfer</summary>
|
||||||
|
/// <param name="original">Original instance that will be cloned</param>
|
||||||
|
/// <returns>A clone of the original instance</returns>
|
||||||
|
private static object deepCloneComplexPropertyBased(object original) {
|
||||||
|
Type originalType = original.GetType();
|
||||||
|
object clone = Activator.CreateInstance(originalType);
|
||||||
|
|
||||||
|
PropertyInfo[] propertyInfos = originalType.GetProperties(
|
||||||
BindingFlags.Public | BindingFlags.NonPublic |
|
BindingFlags.Public | BindingFlags.NonPublic |
|
||||||
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
||||||
);
|
);
|
||||||
for(int index = 0; index < propertyInfos.Length; ++index) {
|
for(int index = 0; index < propertyInfos.Length; ++index) {
|
||||||
PropertyInfo propertyInfo = propertyInfos[index];
|
PropertyInfo propertyInfo = propertyInfos[index];
|
||||||
if(propertyInfo.CanRead && propertyInfo.CanWrite) {
|
Type propertyType = propertyInfo.PropertyType;
|
||||||
if(propertyInfo.PropertyType.IsPrimitive) {
|
object originalValue = propertyInfo.GetValue(original, null);
|
||||||
|
if(originalValue != null) {
|
||||||
|
if(propertyType.IsPrimitive) { // Primitive types can be assigned directly
|
||||||
|
propertyInfo.SetValue(clone, originalValue, null);
|
||||||
|
} else if(propertyType.IsArray) { // Arrays need to be cloned element-by-element
|
||||||
propertyInfo.SetValue(
|
propertyInfo.SetValue(
|
||||||
boxedTarget,
|
clone,
|
||||||
propertyInfo.GetValue(boxedOriginal, null),
|
deepCloneArrayPropertyBased((Array)originalValue, propertyType.GetElementType()),
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
} else if(propertyInfo.PropertyType.IsValueType) {
|
} else { // Complex types need to be cloned member-by-member
|
||||||
object boxedOriginalValue = propertyInfo.GetValue(boxedOriginal, null);
|
propertyInfo.SetValue(clone, deepCloneSinglePropertyBased(originalValue), null);
|
||||||
Type originalType = boxedOriginalValue.GetType();
|
|
||||||
object boxedClonedValue = Activator.CreateInstance(originalType);
|
|
||||||
deepCopyReferenceTypePropertyBased(
|
|
||||||
originalType, boxedOriginalValue, boxedClonedValue
|
|
||||||
);
|
|
||||||
propertyInfo.SetValue(boxedTarget, boxedClonedValue, null);
|
|
||||||
} else {
|
|
||||||
object originalValue = propertyInfo.GetValue(boxedOriginal, null);
|
|
||||||
if(originalValue != null) {
|
|
||||||
Type originalType = originalValue.GetType();
|
|
||||||
object clonedValue = Activator.CreateInstance(originalType);
|
|
||||||
deepCopyReferenceTypePropertyBased(originalType, originalValue, clonedValue);
|
|
||||||
propertyInfo.SetValue(boxedTarget, clonedValue, null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
target = (TState)boxedTarget;
|
return clone;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Clones an array using property-based value transfer</summary>
|
||||||
|
/// <param name="original">Original array that will be cloned</param>
|
||||||
|
/// <param name="elementType">Type of elements the original array contains</param>
|
||||||
|
/// <returns>A clone of the original array</returns>
|
||||||
|
private static object deepCloneArrayPropertyBased(Array original, Type elementType) {
|
||||||
|
if(elementType.IsPrimitive) {
|
||||||
|
return original.Clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
int dimensionCount = original.Rank;
|
||||||
|
|
||||||
|
// Find out the length of each of the array's dimensions, also calculate how
|
||||||
|
// many elements there are in the array in total.
|
||||||
|
var lengths = new int[dimensionCount];
|
||||||
|
int totalElementCount = 0;
|
||||||
|
for(int index = 0; index < dimensionCount; ++index) {
|
||||||
|
lengths[index] = original.GetLength(index);
|
||||||
|
if(index == 0) {
|
||||||
|
totalElementCount = lengths[index];
|
||||||
|
} else {
|
||||||
|
totalElementCount *= lengths[index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Knowing the number of dimensions and the length of each dimension, we can
|
||||||
|
// create another array of the exact same sizes.
|
||||||
|
Array clone = Array.CreateInstance(elementType, lengths);
|
||||||
|
|
||||||
|
// If this is a one-dimensional array (most common type), do an optimized copy
|
||||||
|
// directly specifying the indices
|
||||||
|
if(dimensionCount == 1) {
|
||||||
|
|
||||||
|
// Clone each element of the array directly
|
||||||
|
for(int index = 0; index < totalElementCount; ++index) {
|
||||||
|
object originalElement = original.GetValue(index);
|
||||||
|
if(originalElement != null) {
|
||||||
|
clone.SetValue(deepCloneSinglePropertyBased(originalElement), index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else { // Otherwise use the generic code for multi-dimensional arrays
|
||||||
|
|
||||||
|
var indices = new int[dimensionCount];
|
||||||
|
for(int index = 0; index < totalElementCount; ++index) {
|
||||||
|
|
||||||
|
// Determine the index for each of the array's dimensions
|
||||||
|
int elementIndex = index;
|
||||||
|
for(int dimensionIndex = dimensionCount - 1; dimensionIndex >= 0; --dimensionIndex) {
|
||||||
|
indices[dimensionIndex] = elementIndex % lengths[dimensionIndex];
|
||||||
|
elementIndex /= lengths[dimensionIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clone the current array element
|
||||||
|
object originalElement = original.GetValue(indices);
|
||||||
|
if(originalElement != null) {
|
||||||
|
clone.SetValue(deepCloneSinglePropertyBased(originalElement), indices);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user