From 97de199705c80b492d3c9ecd8a8a773d35886b90 Mon Sep 17 00:00:00 2001 From: Markus Ewald Date: Thu, 8 Mar 2012 12:05:59 +0000 Subject: [PATCH] It seems that contrary to what MSDN says, the Dictionary class does not have a constructor accepting an IEqualityComparer on the Xbox 360 or Windows Phone 7; added missing documentation in Pool class git-svn-id: file:///srv/devel/repo-conversion/nusu@270 d2e56fa2-650e-0410-a79f-9358c0239efd --- Nuclex.Support (net-4.0).csproj | 2 +- Nuclex.Support (xna-4.0-phone7).csproj | 2 +- Nuclex.Support (xna-4.0-xbox360).csproj | 2 +- Source/Collections/Pool.cs | 2 + Source/TypeHelper.cs | 135 ++++++++++++++---------- 5 files changed, 85 insertions(+), 58 deletions(-) diff --git a/Nuclex.Support (net-4.0).csproj b/Nuclex.Support (net-4.0).csproj index 38771f8..36d30b0 100644 --- a/Nuclex.Support (net-4.0).csproj +++ b/Nuclex.Support (net-4.0).csproj @@ -279,7 +279,7 @@ - \TypeHelper.cs + TypeHelper.cs diff --git a/Nuclex.Support (xna-4.0-phone7).csproj b/Nuclex.Support (xna-4.0-phone7).csproj index 27676ea..b6d1d9f 100644 --- a/Nuclex.Support (xna-4.0-phone7).csproj +++ b/Nuclex.Support (xna-4.0-phone7).csproj @@ -310,7 +310,7 @@ - \TypeHelper.cs + TypeHelper.cs diff --git a/Nuclex.Support (xna-4.0-xbox360).csproj b/Nuclex.Support (xna-4.0-xbox360).csproj index 44ac1db..b20a931 100644 --- a/Nuclex.Support (xna-4.0-xbox360).csproj +++ b/Nuclex.Support (xna-4.0-xbox360).csproj @@ -321,7 +321,7 @@ - \TypeHelper.cs + TypeHelper.cs diff --git a/Source/Collections/Pool.cs b/Source/Collections/Pool.cs index 1fd151a..d7c72aa 100644 --- a/Source/Collections/Pool.cs +++ b/Source/Collections/Pool.cs @@ -166,7 +166,9 @@ namespace Nuclex.Support.Collections { /// Required because the Queue class doesn't allow this value to be retrieved /// private int capacity; + /// Delegate used to create new instances of the pool's type private Func createNewDelegate; + /// Delegate used to recycle instances private Action recycleDelegate; } diff --git a/Source/TypeHelper.cs b/Source/TypeHelper.cs index 4601898..0ceee0f 100644 --- a/Source/TypeHelper.cs +++ b/Source/TypeHelper.cs @@ -27,6 +27,8 @@ namespace Nuclex.Support { /// Helper methods for the reflection Type class public static class TypeHelper { +#if !(XBOX360 || WINDOWS_PHONE) + #region class MemberInfoComparer /// Determines whether member informations relate to the same member @@ -58,60 +60,6 @@ namespace Nuclex.Support { #endregion // class MemberInfoComparer - /// Determines whether the given type has a default constructor - /// Type which is to be checked - /// True if the type has a default constructor - public static bool HasDefaultConstructor(this Type type) { - ConstructorInfo[] constructors = type.GetConstructors(); - - for(int index = 0; index < constructors.Length; ++index) { - ConstructorInfo constructor = constructors[index]; - if(constructor.IsPublic && (constructor.GetParameters().Length == 0)) { - return true; - } - } - - return false; - } - -#if XBOX || WINDOWS_PHONE - /// - /// Returns all the fields of a type, including those defined in the type's base classes - /// - /// Type whose fields will be returned - /// Binding flags to use when querying the fields - /// All of the type's fields, including its base types - public static FieldInfo[] GetFieldInfosIncludingBaseClasses( - this 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)) { - var fieldInfoSet = new Dictionary(MemberInfoComparer.Default); - for(int index = 0; index < fieldInfos.Length; ++index) { - fieldInfoSet.Add(fieldInfos[index], null); - } - - while(type.BaseType != typeof(object)) { - type = type.BaseType; - fieldInfos = type.GetFields(bindingFlags); - - for(int index = 0; index < fieldInfos.Length; ++index) { - FieldInfo fieldInfo = fieldInfos[index]; - if(!fieldInfoSet.ContainsKey(fieldInfo)) { - fieldInfoSet.Add(fieldInfo, null); - } - } - } - - fieldInfos = new FieldInfo[fieldInfoSet.Count]; - fieldInfoSet.Keys.CopyTo(fieldInfos, 0); - } - - return fieldInfos; - } -#else /// /// Returns all the fields of a type, including those defined in the type's base classes /// @@ -140,7 +88,84 @@ namespace Nuclex.Support { return fieldInfos; } -#endif + +#else // !(XBOX360 || WINDOWS_PHONE) + + /// + /// Returns all the fields of a type, including those defined in the type's base classes + /// + /// Type whose fields will be returned + /// Binding flags to use when querying the fields + /// All of the type's fields, including its base types + public static FieldInfo[] GetFieldInfosIncludingBaseClasses( + this 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)) { + var fieldInfoList = new List(fieldInfos.Length * 2); + for(int index = 0; index < fieldInfos.Length; ++index) { + fieldInfoList.Add(fieldInfos[index]); + } + + while(type.BaseType != typeof(object)) { + type = type.BaseType; + fieldInfos = type.GetFields(bindingFlags); + + for(int index = 0; index < fieldInfos.Length; ++index) { + addIfNotExists(fieldInfoList, fieldInfos[index]); + } + } + + fieldInfos = fieldInfoList.ToArray(); + } + + return fieldInfos; + } + + /// + /// Adds field informations to a list if they're not already contained in it + /// + /// List the field informations will be added to + /// Field informations that will be added to the list + private static void addIfNotExists(IList fieldInfos, FieldInfo fieldInfo) { + bool matchFound = false; + + for(int index = 0; index < fieldInfos.Count; ++index) { + FieldInfo current = fieldInfos[index]; + + matchFound = + (current.DeclaringType == fieldInfo.DeclaringType) && + (current.Name == fieldInfo.Name); + + if(matchFound) { + break; + } + } + + if(!matchFound) { + fieldInfos.Add(fieldInfo); + } + } + +#endif // !(XBOX360 || WINDOWS_PHONE) + + /// Determines whether the given type has a default constructor + /// Type which is to be checked + /// True if the type has a default constructor + public static bool HasDefaultConstructor(this Type type) { + ConstructorInfo[] constructors = type.GetConstructors(); + + for(int index = 0; index < constructors.Length; ++index) { + ConstructorInfo constructor = constructors[index]; + if(constructor.IsPublic && (constructor.GetParameters().Length == 0)) { + return true; + } + } + + return false; + } }