Windows Phone 7 and Xbox 360 builds now use a Dictionary again

git-svn-id: file:///srv/devel/repo-conversion/nusu@272 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2012-03-09 07:05:38 +00:00
parent 0637b9d71b
commit 9fcee1e872

View File

@ -27,39 +27,39 @@ namespace Nuclex.Support {
/// <summary>Helper methods for the reflection Type class</summary> /// <summary>Helper methods for the reflection Type class</summary>
public static class TypeHelper { public static class TypeHelper {
#if !(XBOX360 || WINDOWS_PHONE) #region class FieldInfoComparer
#region class MemberInfoComparer
/// <summary>Determines whether member informations relate to the same member</summary> /// <summary>Determines whether member informations relate to the same member</summary>
private class MemberInfoComparer : IEqualityComparer<MemberInfo> { private class FieldInfoComparer : IEqualityComparer<FieldInfo> {
/// <summary>Default instance of the comparer</summary> /// <summary>Default instance of the comparer</summary>
public static readonly MemberInfoComparer Default = new MemberInfoComparer(); public static readonly FieldInfoComparer Default = new FieldInfoComparer();
/// <summary>Checks whether two member informations are equal</summary> /// <summary>Checks whether two member informations are equal</summary>
/// <param name="left">Informations about the left member in the comaprison</param> /// <param name="left">Informations about the left member in the comaprison</param>
/// <param name="right">Informations about the right member in the comparison</param> /// <param name="right">Informations about the right member in the comparison</param>
/// <returns>True if the two member informations relate to the same member</returns> /// <returns>True if the two member informations relate to the same member</returns>
public bool Equals(MemberInfo left, MemberInfo right) { public bool Equals(FieldInfo left, FieldInfo right) {
return return
(left.DeclaringType == right.DeclaringType) && (left.DeclaringType == right.DeclaringType) &&
(left.Name == right.Name); (left.Name == right.Name);
} }
/// <summary>Determines the hash code of the specified member informations</summary> /// <summary>Determines the hash code of the specified member informations</summary>
/// <param name="memberInfo"> /// <param name="FieldInfo">
/// Member informations whose hash code will be determined /// Member informations whose hash code will be determined
/// </param> /// </param>
/// <returns>The hash code of the specified member informations</returns> /// <returns>The hash code of the specified member informations</returns>
public int GetHashCode(MemberInfo memberInfo) { public int GetHashCode(FieldInfo FieldInfo) {
return (memberInfo.DeclaringType.GetHashCode() ^ memberInfo.Name.GetHashCode()); return (FieldInfo.DeclaringType.GetHashCode() ^ FieldInfo.Name.GetHashCode());
} }
} }
#endregion // class MemberInfoComparer #endregion // class MemberInfoComparer
#if !(XBOX360 || WINDOWS_PHONE)
/// <summary> /// <summary>
/// Returns all the fields of a type, including those defined in the type's base classes /// Returns all the fields of a type, including those defined in the type's base classes
/// </summary> /// </summary>
@ -104,9 +104,9 @@ namespace Nuclex.Support {
// If this class doesn't have a base, don't waste any time // If this class doesn't have a base, don't waste any time
if(type.BaseType != typeof(object)) { if(type.BaseType != typeof(object)) {
var fieldInfoList = new List<FieldInfo>(fieldInfos.Length * 2); var fieldInfoSet = new Dictionary<FieldInfo, object>(FieldInfoComparer.Default);
for(int index = 0; index < fieldInfos.Length; ++index) { for(int index = 0; index < fieldInfos.Length; ++index) {
fieldInfoList.Add(fieldInfos[index]); fieldInfoSet.Add(fieldInfos[index], null);
} }
while(type.BaseType != typeof(object)) { while(type.BaseType != typeof(object)) {
@ -114,11 +114,12 @@ namespace Nuclex.Support {
fieldInfos = type.GetFields(bindingFlags); fieldInfos = type.GetFields(bindingFlags);
for(int index = 0; index < fieldInfos.Length; ++index) { for(int index = 0; index < fieldInfos.Length; ++index) {
addIfNotExists(fieldInfoList, fieldInfos[index]); addIfNotExists(fieldInfoSet, fieldInfos[index]);
} }
} }
fieldInfos = fieldInfoList.ToArray(); fieldInfos = new FieldInfo[fieldInfoSet.Count];
fieldInfoSet.Keys.CopyTo(fieldInfos, 0);
} }
return fieldInfos; return fieldInfos;
@ -129,23 +130,11 @@ namespace Nuclex.Support {
/// </summary> /// </summary>
/// <param name="fieldInfos">List the field informations will be added to</param> /// <param name="fieldInfos">List the field informations will be added to</param>
/// <param name="fieldInfo">Field informations that will be added to the list</param> /// <param name="fieldInfo">Field informations that will be added to the list</param>
private static void addIfNotExists(IList<FieldInfo> fieldInfos, FieldInfo fieldInfo) { private static void addIfNotExists(
bool matchFound = false; IDictionary<FieldInfo, object> fieldInfos, FieldInfo fieldInfo
) {
for(int index = 0; index < fieldInfos.Count; ++index) { if(!fieldInfos.ContainsKey(fieldInfo)) {
FieldInfo current = fieldInfos[index]; fieldInfos.Add(fieldInfo, null);
matchFound =
(current.DeclaringType == fieldInfo.DeclaringType) &&
(current.Name == fieldInfo.Name);
if(matchFound) {
break;
}
}
if(!matchFound) {
fieldInfos.Add(fieldInfo);
} }
} }