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) {
|
if(usePropertyBasedClone) {
|
||||||
TCloned clone = Activator.CreateInstance<TCloned>();
|
return (TCloned)deepCloneSinglePropertyBased(objectToClone);
|
||||||
if(usePropertyBasedClone) {
|
|
||||||
deepCopyValueTypePropertyBased<TCloned>(ref objectToClone, ref clone);
|
|
||||||
} else {
|
|
||||||
deepCopyValueTypeFieldBased<TCloned>(ref objectToClone, ref clone);
|
|
||||||
}
|
|
||||||
return clone;
|
|
||||||
} else {
|
} else {
|
||||||
TCloned clone = (TCloned)Activator.CreateInstance(objectToClone.GetType());
|
return (TCloned)deepCloneSingleFieldBased(objectToClone);
|
||||||
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));
|
object originalValue = fieldInfo.GetValue(original);
|
||||||
|
if(originalValue != null) {
|
||||||
|
// Everything's just directly assigned in a shallow clone
|
||||||
|
fieldInfo.SetValue(clone, originalValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Creates a property-based shallow copy of a reference type</summary>
|
/// <summary>Clones a complex type using property-based value transfer</summary>
|
||||||
/// <param name="copiedType">Type the copy will be based upon</param>
|
/// <param name="original">Original instance that will be cloned</param>
|
||||||
/// <param name="original">Original object that will be copied</param>
|
/// <returns>A clone of the original instance</returns>
|
||||||
/// <param name="target">Target object into which copied values will be written</param>
|
private object shallowCloneComplexPropertyBased(object original) {
|
||||||
private static void shallowCopyReferenceTypePropertyBased(
|
Type originalType = original.GetType();
|
||||||
Type copiedType, object original, object target
|
object clone = Activator.CreateInstance(originalType);
|
||||||
) {
|
|
||||||
PropertyInfo[] propertyInfos = copiedType.GetProperties(
|
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);
|
||||||
propertyInfo.SetValue(
|
if(originalValue != null) {
|
||||||
target,
|
if(propertyType.IsPrimitive) { // Primitive types can be assigned directly
|
||||||
propertyInfo.GetValue(original, null),
|
propertyInfo.SetValue(clone, originalValue, null);
|
||||||
null
|
} else if(propertyType.IsValueType) {
|
||||||
);
|
// Value types are seen as part of the original type and are thus recursed into
|
||||||
} else if(propertyInfo.PropertyType.IsValueType) {
|
propertyInfo.SetValue(clone, shallowCloneComplexPropertyBased(originalValue), null);
|
||||||
// Recurse into the value type - value types are seen as part of
|
} else if(propertyType.IsArray) { // Arrays are assigned directly in a shallow clone
|
||||||
// the outer type and a shallow copy does follow their hierarchy.
|
propertyInfo.SetValue(clone, originalValue, null);
|
||||||
// This is equivalent to what would happen if you directly assign
|
} else { // Complex types are directly assigned without creating a copy
|
||||||
// one value type to another.
|
propertyInfo.SetValue(clone, originalValue, null);
|
||||||
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;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Creates a field-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>
|
/// <returns>A clone of the original array</returns>
|
||||||
/// <param name="target">Target object into which copied values will be written</param>
|
private object shallowCloneArray(object original) {
|
||||||
private static void shallowCopyValueTypeFieldBased<TState>(
|
return ((Array)original).Clone();
|
||||||
ref TState original, ref TState target
|
|
||||||
) {
|
|
||||||
target = original; // hehe
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Creates a field-based deep copy of a reference type</summary>
|
/// <summary>Copies a single object using field-based value transfer</summary>
|
||||||
/// <param name="copiedType">Type the copy will be based upon</param>
|
/// <param name="original">Original object that will be cloned</param>
|
||||||
/// <param name="original">Original object that will be copied</param>
|
/// <returns>A clone of the original object</returns>
|
||||||
/// <param name="target">Target object into which copied values will be written</param>
|
private static object deepCloneSingleFieldBased(object original) {
|
||||||
private static void deepCopyReferenceTypeFieldBased(
|
Type originalType = original.GetType();
|
||||||
Type copiedType, object original, object target
|
if(originalType.IsPrimitive) {
|
||||||
) {
|
return original; // Creates another box, does not reference boxed primitive
|
||||||
FieldInfo[] fieldInfos = copiedType.GetFields(
|
} 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;
|
||||||
fieldInfo.SetValue(target, fieldInfo.GetValue(original));
|
object originalValue = fieldInfo.GetValue(original);
|
||||||
} else if(fieldInfo.FieldType.IsValueType) {
|
if(originalValue != null) {
|
||||||
object boxedOriginalValue = fieldInfo.GetValue(original);
|
if(fieldType.IsPrimitive) { // Primitive types can be assigned directly
|
||||||
Type originalType = boxedOriginalValue.GetType();
|
fieldInfo.SetValue(clone, originalValue);
|
||||||
object boxedClonedValue = Activator.CreateInstance(originalType);
|
} else if(fieldType.IsArray) { // Arrays need to be cloned element-by-element
|
||||||
deepCopyReferenceTypeFieldBased(originalType, boxedOriginalValue, boxedClonedValue);
|
fieldInfo.SetValue(
|
||||||
fieldInfo.SetValue(target, boxedClonedValue);
|
clone,
|
||||||
} else {
|
deepCloneArrayFieldBased((Array)originalValue, fieldType.GetElementType())
|
||||||
object originalValue = fieldInfo.GetValue(original);
|
);
|
||||||
if(originalValue != null) {
|
} else { // Complex types need to be cloned member-by-member
|
||||||
Type originalType = originalValue.GetType();
|
fieldInfo.SetValue(clone, deepCloneSingleFieldBased(originalValue));
|
||||||
object clonedValue = Activator.CreateInstance(originalType);
|
|
||||||
deepCopyReferenceTypeFieldBased(originalType, originalValue, clonedValue);
|
|
||||||
fieldInfo.SetValue(target, clonedValue);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Creates a property-based deep copy of a reference type</summary>
|
/// <summary>Clones an array using field-based value transfer</summary>
|
||||||
/// <param name="copiedType">Type the copy will be based upon</param>
|
/// <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 deepCopyReferenceTypePropertyBased(
|
private static object deepCloneArrayFieldBased(Array original, Type elementType) {
|
||||||
Type copiedType, object original, object target
|
if(elementType.IsPrimitive) {
|
||||||
) {
|
return original.Clone();
|
||||||
PropertyInfo[] propertyInfos = copiedType.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(
|
||||||
target,
|
clone,
|
||||||
propertyInfo.GetValue(original, 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(original, null);
|
propertyInfo.SetValue(clone, deepCloneSinglePropertyBased(originalValue), 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);
|
|
||||||
if(originalValue != null) {
|
|
||||||
Type originalType = originalValue.GetType();
|
|
||||||
object clonedValue = Activator.CreateInstance(originalType);
|
|
||||||
deepCopyReferenceTypePropertyBased(
|
|
||||||
originalType, originalValue, clonedValue
|
|
||||||
);
|
|
||||||
propertyInfo.SetValue(target, clonedValue, null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Creates a field-based deep copy of a value type</summary>
|
/// <summary>Clones an array using property-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 deepCopyValueTypeFieldBased<TState>(
|
private static object deepCloneArrayPropertyBased(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());
|
|
||||||
|
|
||||||
FieldInfo[] fieldInfos = typeof(TState).GetFields(
|
int dimensionCount = original.Rank;
|
||||||
BindingFlags.Public | BindingFlags.NonPublic |
|
|
||||||
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
// Find out the length of each of the array's dimensions, also calculate how
|
||||||
);
|
// many elements there are in the array in total.
|
||||||
for(int index = 0; index < fieldInfos.Length; ++index) {
|
var lengths = new int[dimensionCount];
|
||||||
FieldInfo fieldInfo = fieldInfos[index];
|
int totalElementCount = 0;
|
||||||
if(fieldInfo.FieldType.IsPrimitive) {
|
for(int index = 0; index < dimensionCount; ++index) {
|
||||||
object boxedValue = fieldInfo.GetValue(boxedOriginal);
|
lengths[index] = original.GetLength(index);
|
||||||
fieldInfo.SetValue(boxedTarget, boxedValue);
|
if(index == 0) {
|
||||||
} else if(fieldInfo.FieldType.IsValueType) {
|
totalElementCount = lengths[index];
|
||||||
object boxedOriginalValue = fieldInfo.GetValue(boxedOriginal);
|
|
||||||
Type originalType = boxedOriginalValue.GetType();
|
|
||||||
object boxedClonedValue = Activator.CreateInstance(originalType);
|
|
||||||
deepCopyReferenceTypeFieldBased(originalType, boxedOriginalValue, boxedClonedValue);
|
|
||||||
fieldInfo.SetValue(boxedTarget, boxedClonedValue);
|
|
||||||
} else {
|
} else {
|
||||||
object originalValue = fieldInfo.GetValue(boxedOriginal);
|
totalElementCount *= lengths[index];
|
||||||
if(originalValue != null) {
|
|
||||||
Type originalType = originalValue.GetType();
|
|
||||||
object clonedValue = Activator.CreateInstance(originalType);
|
|
||||||
deepCopyReferenceTypeFieldBased(originalType, originalValue, clonedValue);
|
|
||||||
fieldInfo.SetValue(boxedTarget, clonedValue);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
target = (TState)boxedTarget;
|
// 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);
|
||||||
|
|
||||||
/// <summary>Creates a property-based shallow copy of a value type</summary>
|
// If this is a one-dimensional array (most common type), do an optimized copy
|
||||||
/// <typeparam name="TState">Value type that will be copied</typeparam>
|
// directly specifying the indices
|
||||||
/// <param name="original">Original object that will be copied</param>
|
if(dimensionCount == 1) {
|
||||||
/// <param name="target">Target object into which copied values will be written</param>
|
|
||||||
private static void deepCopyValueTypePropertyBased<TState>(
|
|
||||||
ref TState original, ref TState target
|
|
||||||
) {
|
|
||||||
object boxedOriginal = original;
|
|
||||||
object boxedTarget = Activator.CreateInstance(original.GetType());
|
|
||||||
|
|
||||||
PropertyInfo[] propertyInfos = typeof(TState).GetProperties(
|
// Clone each element of the array directly
|
||||||
BindingFlags.Public | BindingFlags.NonPublic |
|
for(int index = 0; index < totalElementCount; ++index) {
|
||||||
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
object originalElement = original.GetValue(index);
|
||||||
);
|
if(originalElement != null) {
|
||||||
for(int index = 0; index < propertyInfos.Length; ++index) {
|
clone.SetValue(deepCloneSinglePropertyBased(originalElement), 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(boxedOriginal, 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
target = (TState)boxedTarget;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user