Pool can now be used with types not derived from IRecyclable and/or without a public default constructor; consolidated type-related helper methods into a common helper class (TypeHelper.cs); optimized GetFieldInfosIncludingBaseClasses() method

git-svn-id: file:///srv/devel/repo-conversion/nusu@268 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2012-03-08 11:05:20 +00:00
parent 24439da822
commit c91a082e84
18 changed files with 277 additions and 292 deletions

View file

@ -1,86 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
#if UNITTEST
using System;
using System.Reflection;
using NUnit.Framework;
namespace Nuclex.Support.Cloning {
/// <summary>Unit Test for the cloner helpers</summary>
[TestFixture]
internal class ClonerHelpersTest {
#region class Base
/// <summary>Base class used to test the helper methods</summary>
private class Base {
/// <summary>A simple public field</summary>
public int PublicBaseField;
/// <summary>An automatic property with a hidden backing field</summary>
public int PublicBaseProperty { get; set; }
}
#endregion // class Base
#region class Derived
/// <summary>Derived class used to test the helper methods</summary>
private class Derived : Base {
/// <summary>A simple public field</summary>
public int PublicDerivedField;
/// <summary>An automatic property with a hidden backing field</summary>
public int PublicDerivedProperty { get; set; }
}
#endregion // class Derived
/// <summary>
/// Verifies that the GetFieldInfosIncludingBaseClasses() will include the backing
/// fields of automatically implemented properties in base classes
/// </summary>
[Test]
public void CanGetBackingFieldsForPropertiesInBaseClasses() {
FieldInfo[] fieldInfos = ClonerHelpers.GetFieldInfosIncludingBaseClasses(
typeof(Derived), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
);
Assert.AreEqual(4, fieldInfos.Length);
}
/// <summary>
/// Useless test that avoids a compile warning about unused fields
/// </summary>
[Test]
public void AvoidCompilerWarnings() {
var derived = new Derived() {
PublicBaseField = 123,
PublicBaseProperty = 321,
PublicDerivedField = 456
};
}
}
} // namespace Nuclex.Support.Cloning
#endif // UNITTEST

View file

@ -1,81 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace Nuclex.Support.Cloning {
/// <summary>Contains helper methods for the cloners</summary>
internal static class ClonerHelpers {
/// <summary>
/// Returns all the fields of a type, working around a weird reflection issue
/// where explicitly declared fields in base classes are returned, but not
/// automatic property backing fields.
/// </summary>
/// <param name="type">Type whose fields will be returned</param>
/// <param name="bindingFlags">Binding flags to use when querying the fields</param>
/// <returns>All of the type's fields, including its base types</returns>
public static FieldInfo[] GetFieldInfosIncludingBaseClasses(
Type type, BindingFlags bindingFlags
) {
FieldInfo[] fieldInfos = type.GetFields(bindingFlags);
// If this class doesn't have a base, don't waste any time
if(type.BaseType == typeof(object)) {
return fieldInfos;
} else { // Otherwise, collect all types up to the furthest base class
var fieldInfoList = new List<FieldInfo>(fieldInfos);
while(type.BaseType != typeof(object)) {
type = type.BaseType;
fieldInfos = type.GetFields(bindingFlags);
// Look for fields we do not have listed yet and merge them into the main list
for(int index = 0; index < fieldInfos.Length; ++index) {
bool found = false;
for(int searchIndex = 0; searchIndex < fieldInfoList.Count; ++searchIndex) {
bool match =
(fieldInfoList[searchIndex].DeclaringType == fieldInfos[index].DeclaringType) &&
(fieldInfoList[searchIndex].Name == fieldInfos[index].Name);
if(match) {
found = true;
break;
}
}
if(!found) {
fieldInfoList.Add(fieldInfos[index]);
}
}
}
return fieldInfoList.ToArray();
}
}
}
} // namespace Nuclex.Support.Cloning

View file

@ -185,8 +185,8 @@ namespace Nuclex.Support.Cloning {
);
// Enumerate all of the type's fields and generate transfer expressions for each
FieldInfo[] fieldInfos = ClonerHelpers.GetFieldInfosIncludingBaseClasses(
clonedType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
FieldInfo[] fieldInfos = clonedType.GetFieldInfosIncludingBaseClasses(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
);
for(int index = 0; index < fieldInfos.Length; ++index) {
FieldInfo fieldInfo = fieldInfos[index];
@ -495,8 +495,8 @@ namespace Nuclex.Support.Cloning {
ICollection<Expression> transferExpressions
) {
// Enumerate all of the type's fields and generate transfer expressions for each
FieldInfo[] fieldInfos = ClonerHelpers.GetFieldInfosIncludingBaseClasses(
clonedType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
FieldInfo[] fieldInfos = clonedType.GetFieldInfosIncludingBaseClasses(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
);
for(int index = 0; index < fieldInfos.Length; ++index) {
FieldInfo fieldInfo = fieldInfos[index];

View file

@ -167,8 +167,8 @@ namespace Nuclex.Support.Cloning {
object clone = FormatterServices.GetUninitializedObject(originalType);
#endif
FieldInfo[] fieldInfos = ClonerHelpers.GetFieldInfosIncludingBaseClasses(
originalType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
FieldInfo[] fieldInfos = originalType.GetFieldInfosIncludingBaseClasses(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
);
for(int index = 0; index < fieldInfos.Length; ++index) {
FieldInfo fieldInfo = fieldInfos[index];
@ -249,8 +249,8 @@ namespace Nuclex.Support.Cloning {
object clone = FormatterServices.GetUninitializedObject(originalType);
#endif
FieldInfo[] fieldInfos = ClonerHelpers.GetFieldInfosIncludingBaseClasses(
originalType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
FieldInfo[] fieldInfos = originalType.GetFieldInfosIncludingBaseClasses(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
);
for(int index = 0; index < fieldInfos.Length; ++index) {
FieldInfo fieldInfo = fieldInfos[index];

View file

@ -118,8 +118,8 @@ namespace Nuclex.Support.Cloning {
) {
Type originalType = objectToSerialize.GetType();
FieldInfo[] fieldInfos = ClonerHelpers.GetFieldInfosIncludingBaseClasses(
originalType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
FieldInfo[] fieldInfos = originalType.GetFieldInfosIncludingBaseClasses(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
);
for(int index = 0; index < fieldInfos.Length; ++index) {
FieldInfo fieldInfo = fieldInfos[index];
@ -143,8 +143,8 @@ namespace Nuclex.Support.Cloning {
) {
Type originalType = deserializedObject.GetType();
FieldInfo[] fieldInfos = ClonerHelpers.GetFieldInfosIncludingBaseClasses(
originalType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
FieldInfo[] fieldInfos = originalType.GetFieldInfosIncludingBaseClasses(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
);
for(int index = 0; index < fieldInfos.Length; ++index) {
FieldInfo fieldInfo = fieldInfos[index];