Fixed a bug in the cloners: if a class has properties with only a setter or only a getter, the property-based clones now ignore that property; added a unit test that verifies types without a default constructor can be cloned as well
git-svn-id: file:///srv/devel/repo-conversion/nusu@248 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
48e1674912
commit
0290444140
|
@ -78,6 +78,14 @@ namespace Nuclex.Support.Cloning {
|
||||||
public TestReferenceType[,][] ReferenceTypeArrayField;
|
public TestReferenceType[,][] ReferenceTypeArrayField;
|
||||||
/// <summary>An array property of reference types</summary>
|
/// <summary>An array property of reference types</summary>
|
||||||
public TestReferenceType[,][] ReferenceTypeArrayProperty { get; set; }
|
public TestReferenceType[,][] ReferenceTypeArrayProperty { get; set; }
|
||||||
|
/// <summary>A reference type field that's always null</summary>
|
||||||
|
public TestReferenceType AlwaysNullField;
|
||||||
|
/// <summary>A reference type property that's always null</summary>
|
||||||
|
public TestReferenceType AlwaysNullProperty { get; set; }
|
||||||
|
/// <summary>A property that only has a getter</summary>
|
||||||
|
public TestReferenceType GetOnlyProperty { get { return null; } }
|
||||||
|
/// <summary>A property that only has a s</summary>
|
||||||
|
public TestReferenceType SetOnlyProperty { set { } }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,11 +112,44 @@ namespace Nuclex.Support.Cloning {
|
||||||
public TestReferenceType[,][] ReferenceTypeArrayField;
|
public TestReferenceType[,][] ReferenceTypeArrayField;
|
||||||
/// <summary>An array property of reference types</summary>
|
/// <summary>An array property of reference types</summary>
|
||||||
public TestReferenceType[,][] ReferenceTypeArrayProperty { get; set; }
|
public TestReferenceType[,][] ReferenceTypeArrayProperty { get; set; }
|
||||||
|
/// <summary>A reference type field that's always null</summary>
|
||||||
|
public TestReferenceType AlwaysNullField;
|
||||||
|
/// <summary>A reference type property that's always null</summary>
|
||||||
|
public TestReferenceType AlwaysNullProperty { get; set; }
|
||||||
|
/// <summary>A property that only has a getter</summary>
|
||||||
|
public TestReferenceType GetOnlyProperty { get { return null; } }
|
||||||
|
/// <summary>A property that only has a s</summary>
|
||||||
|
public TestReferenceType SetOnlyProperty { set { } }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion // struct HierarchicalReferenceType
|
#endregion // struct HierarchicalReferenceType
|
||||||
|
|
||||||
|
#region class ClassWithoutDefaultConstructor
|
||||||
|
|
||||||
|
/// <summary>A class that does not have a default constructor</summary>
|
||||||
|
public class ClassWithoutDefaultConstructor {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the class without default constructor
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dummy">Dummy value that will be saved by the instance</param>
|
||||||
|
public ClassWithoutDefaultConstructor(int dummy) {
|
||||||
|
this.dummy = dummy;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Dummy value that has been saved by the instance</summary>
|
||||||
|
public int Dummy {
|
||||||
|
get { return this.dummy; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Dummy value that has been saved by the instance</summary>
|
||||||
|
private int dummy;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion // class ClassWithoutDefaultConstructor
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Verifies that a cloned object exhibits the expected state for the type of
|
/// Verifies that a cloned object exhibits the expected state for the type of
|
||||||
/// clone that has been performed
|
/// clone that has been performed
|
||||||
|
|
|
@ -213,6 +213,7 @@ namespace Nuclex.Support.Cloning {
|
||||||
);
|
);
|
||||||
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;
|
Type propertyType = propertyInfo.PropertyType;
|
||||||
|
|
||||||
if(propertyType.IsPrimitive || (propertyType == typeof(string))) {
|
if(propertyType.IsPrimitive || (propertyType == typeof(string))) {
|
||||||
|
@ -254,6 +255,7 @@ namespace Nuclex.Support.Cloning {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generates state transfer expressions to copy an array of primitive types
|
/// Generates state transfer expressions to copy an array of primitive types
|
||||||
|
@ -503,6 +505,7 @@ namespace Nuclex.Support.Cloning {
|
||||||
);
|
);
|
||||||
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;
|
Type propertyType = propertyInfo.PropertyType;
|
||||||
|
|
||||||
if(propertyType.IsPrimitive || (propertyType == typeof(string))) {
|
if(propertyType.IsPrimitive || (propertyType == typeof(string))) {
|
||||||
|
@ -552,6 +555,7 @@ namespace Nuclex.Support.Cloning {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generates the expressions to transfer a reference type (array or class)
|
/// Generates the expressions to transfer a reference type (array or class)
|
||||||
|
|
|
@ -45,6 +45,19 @@ namespace Nuclex.Support.Cloning {
|
||||||
Assert.IsNull(this.cloneFactory.ShallowPropertyClone<object>(null));
|
Assert.IsNull(this.cloneFactory.ShallowPropertyClone<object>(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that clones of objects whose class doesn't possess a default constructor
|
||||||
|
/// can be made
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void ClassWithoutDefaultConstructorCanBeCloned() {
|
||||||
|
var original = new ClassWithoutDefaultConstructor(1234);
|
||||||
|
ClassWithoutDefaultConstructor clone = this.cloneFactory.DeepFieldClone(original);
|
||||||
|
|
||||||
|
Assert.AreNotSame(original, clone);
|
||||||
|
Assert.AreEqual(original.Dummy, clone.Dummy);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Verifies that clones of primitive types can be created</summary>
|
/// <summary>Verifies that clones of primitive types can be created</summary>
|
||||||
[Test]
|
[Test]
|
||||||
public void PrimitiveTypesCanBeCloned() {
|
public void PrimitiveTypesCanBeCloned() {
|
||||||
|
|
|
@ -45,6 +45,19 @@ namespace Nuclex.Support.Cloning {
|
||||||
Assert.IsNull(this.cloneFactory.ShallowPropertyClone<object>(null));
|
Assert.IsNull(this.cloneFactory.ShallowPropertyClone<object>(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that clones of objects whose class doesn't possess a default constructor
|
||||||
|
/// can be made
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void ClassWithoutDefaultConstructorCanBeCloned() {
|
||||||
|
var original = new ClassWithoutDefaultConstructor(1234);
|
||||||
|
ClassWithoutDefaultConstructor clone = this.cloneFactory.DeepFieldClone(original);
|
||||||
|
|
||||||
|
Assert.AreNotSame(original, clone);
|
||||||
|
Assert.AreEqual(original.Dummy, clone.Dummy);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Verifies that clones of primitive types can be created</summary>
|
/// <summary>Verifies that clones of primitive types can be created</summary>
|
||||||
[Test]
|
[Test]
|
||||||
public void PrimitiveTypesCanBeCloned() {
|
public void PrimitiveTypesCanBeCloned() {
|
||||||
|
|
|
@ -196,6 +196,7 @@ namespace Nuclex.Support.Cloning {
|
||||||
);
|
);
|
||||||
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;
|
Type propertyType = propertyInfo.PropertyType;
|
||||||
object originalValue = propertyInfo.GetValue(original, null);
|
object originalValue = propertyInfo.GetValue(original, null);
|
||||||
if(originalValue != null) {
|
if(originalValue != null) {
|
||||||
|
@ -212,6 +213,7 @@ namespace Nuclex.Support.Cloning {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
@ -366,6 +368,7 @@ namespace Nuclex.Support.Cloning {
|
||||||
);
|
);
|
||||||
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;
|
Type propertyType = propertyInfo.PropertyType;
|
||||||
object originalValue = propertyInfo.GetValue(original, null);
|
object originalValue = propertyInfo.GetValue(original, null);
|
||||||
if(originalValue != null) {
|
if(originalValue != null) {
|
||||||
|
@ -383,6 +386,7 @@ namespace Nuclex.Support.Cloning {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,19 @@ namespace Nuclex.Support.Cloning {
|
||||||
Assert.IsNull(this.cloneFactory.DeepPropertyClone<object>(null));
|
Assert.IsNull(this.cloneFactory.DeepPropertyClone<object>(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that clones of objects whose class doesn't possess a default constructor
|
||||||
|
/// can be made
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void ClassWithoutDefaultConstructorCanBeCloned() {
|
||||||
|
var original = new ClassWithoutDefaultConstructor(1234);
|
||||||
|
ClassWithoutDefaultConstructor clone = this.cloneFactory.DeepFieldClone(original);
|
||||||
|
|
||||||
|
Assert.AreNotSame(original, clone);
|
||||||
|
Assert.AreEqual(original.Dummy, clone.Dummy);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Verifies that clones of primitive types can be created</summary>
|
/// <summary>Verifies that clones of primitive types can be created</summary>
|
||||||
[Test]
|
[Test]
|
||||||
public void PrimitiveTypesCanBeCloned() {
|
public void PrimitiveTypesCanBeCloned() {
|
||||||
|
|
|
@ -184,9 +184,11 @@ namespace Nuclex.Support.Cloning {
|
||||||
);
|
);
|
||||||
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) {
|
||||||
info.AddValue(propertyInfo.Name, propertyInfo.GetValue(objectToSerialize, null));
|
info.AddValue(propertyInfo.Name, propertyInfo.GetValue(objectToSerialize, null));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Reinserts saved data into a deserializd object</summary>
|
/// <summary>Reinserts saved data into a deserializd object</summary>
|
||||||
/// <param name="deserializedObject">Object the saved data will be inserted into</param>
|
/// <param name="deserializedObject">Object the saved data will be inserted into</param>
|
||||||
|
@ -210,12 +212,14 @@ namespace Nuclex.Support.Cloning {
|
||||||
);
|
);
|
||||||
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) {
|
||||||
propertyInfo.SetValue(
|
propertyInfo.SetValue(
|
||||||
deserializedObject,
|
deserializedObject,
|
||||||
info.GetValue(propertyInfo.Name, propertyInfo.PropertyType),
|
info.GetValue(propertyInfo.Name, propertyInfo.PropertyType),
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return deserializedObject;
|
return deserializedObject;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user