From 7d4a66e9f2a3119dd1202f95d415d94798499ba0 Mon Sep 17 00:00:00 2001 From: Markus Ewald Date: Sat, 17 Mar 2012 13:03:40 +0000 Subject: [PATCH] Some attempts at making this compile on WinRT. Much work remains. git-svn-id: file:///srv/devel/repo-conversion/nusu@281 d2e56fa2-650e-0410-a79f-9358c0239efd --- Source/Collections/Deque.cs | 5 ++ Source/Collections/IMultiDictionary.cs | 4 ++ .../Collections/ItemReplaceEventArgs.Test.cs | 2 +- .../Collections/MultiDictionary.Interfaces.cs | 24 +++++-- Source/Collections/MultiDictionary.cs | 31 ++++++--- Source/Collections/ObservableDictionary.cs | 32 +++++++--- Source/Collections/ReadOnlyDictionary.cs | 36 ++++++++--- Source/TypeHelper.cs | 63 ++++++++++++++++++- 8 files changed, 163 insertions(+), 34 deletions(-) diff --git a/Source/Collections/Deque.cs b/Source/Collections/Deque.cs index 987cf99..07b19dc 100644 --- a/Source/Collections/Deque.cs +++ b/Source/Collections/Deque.cs @@ -21,6 +21,7 @@ License along with this library using System; using System.Collections.Generic; using System.Collections; +using System.Reflection; namespace Nuclex.Support.Collections { @@ -308,7 +309,11 @@ namespace Nuclex.Support.Collections { /// Value that will be checked for compatibility /// True if the value can be placed in the deque private static bool isCompatibleObject(object value) { +#if WINRT + return ((value is TItem) || ((value == null) && !typeof(TItem).GetTypeInfo().IsValueType)); +#else return ((value is TItem) || ((value == null) && !typeof(TItem).IsValueType)); +#endif } /// Verifies that the provided object matches the deque's type diff --git a/Source/Collections/IMultiDictionary.cs b/Source/Collections/IMultiDictionary.cs index 33d0849..11f1f62 100644 --- a/Source/Collections/IMultiDictionary.cs +++ b/Source/Collections/IMultiDictionary.cs @@ -31,7 +31,11 @@ namespace Nuclex.Support.Collections { /// Type of values stored in the dictionary public interface IMultiDictionary : IDictionary>, +#if WINRT + ICollection, +#else IDictionary, +#endif ICollection>, IEnumerable>, IEnumerable { diff --git a/Source/Collections/ItemReplaceEventArgs.Test.cs b/Source/Collections/ItemReplaceEventArgs.Test.cs index 896f359..dad6f1c 100644 --- a/Source/Collections/ItemReplaceEventArgs.Test.cs +++ b/Source/Collections/ItemReplaceEventArgs.Test.cs @@ -29,7 +29,7 @@ namespace Nuclex.Support.Collections { /// Unit Test for the item event argument container [TestFixture] - public class ItemReplaceEventArgsTest { + internal class ItemReplaceEventArgsTest { /// /// Tests whether an integer argument can be stored in the argument container diff --git a/Source/Collections/MultiDictionary.Interfaces.cs b/Source/Collections/MultiDictionary.Interfaces.cs index 845e62d..bb83db0 100644 --- a/Source/Collections/MultiDictionary.Interfaces.cs +++ b/Source/Collections/MultiDictionary.Interfaces.cs @@ -38,6 +38,8 @@ namespace Nuclex.Support.Collections { #region IDictionary implementation +#if !WINRT + /// Adds an item into the dictionary /// Key under which the item will be added /// Item that will be added @@ -52,12 +54,6 @@ namespace Nuclex.Support.Collections { return this.objectDictionary.Contains(key); } - /// Returns a new entry enumerator for the dictionary - /// The new entry enumerator - IDictionaryEnumerator IDictionary.GetEnumerator() { - return new Enumerator(this); - } - /// Whether the size of the dictionary is fixed bool IDictionary.IsFixedSize { get { return this.objectDictionary.IsFixedSize; } @@ -93,8 +89,24 @@ namespace Nuclex.Support.Collections { set { this[(TKey)key] = (ICollection)value; } } +#endif // !WINRT + #endregion + #region IDictionaryEnumerator implementation + +#if !WINRT + + /// Returns a new entry enumerator for the dictionary + /// The new entry enumerator + IDictionaryEnumerator IDictionary.GetEnumerator() { + return new Enumerator(this); + } + +#endif // !WINRT + + #endregion // IDictionaryEnumerator implementation + #region ICollection> implementation /// Inserts an already prepared element into the dictionary diff --git a/Source/Collections/MultiDictionary.cs b/Source/Collections/MultiDictionary.cs index d627192..098a3fa 100644 --- a/Source/Collections/MultiDictionary.cs +++ b/Source/Collections/MultiDictionary.cs @@ -34,7 +34,10 @@ namespace Nuclex.Support.Collections { /// Enumerates the values stored in a multi dictionary private class Enumerator : - IEnumerator>, IDictionaryEnumerator { +#if !WINRT + IDictionaryEnumerator, +#endif + IEnumerator> { /// Initializes a new multi dictionary enumerator /// Dictionary that will be enumerated @@ -125,6 +128,8 @@ namespace Nuclex.Support.Collections { #region IDictionaryEnumerator implementation +#if !WINRT + /// The current entry the enumerator is pointing to DictionaryEntry IDictionaryEnumerator.Entry { get { @@ -136,15 +141,6 @@ namespace Nuclex.Support.Collections { } } - /// - /// Throws an exception if the enumerator is not on a valid position - /// - private void enforceEnumeratorOnValidPosition() { - if(this.currentValue == null) { - throw new InvalidOperationException("Enumerator is not on a valid position"); - } - } - /// The current dictionary key object IDictionaryEnumerator.Key { get { @@ -161,8 +157,19 @@ namespace Nuclex.Support.Collections { } } +#endif // !WINRT + #endregion // IDictionaryEnumerator implementation + /// + /// Throws an exception if the enumerator is not on a valid position + /// + private void enforceEnumeratorOnValidPosition() { + if(this.currentValue == null) { + throw new InvalidOperationException("Enumerator is not on a valid position"); + } + } + /// Dictionary over whose entries the enumerator is enumerating private IDictionary> dictionary; /// Current key the enumerator is at @@ -220,7 +227,9 @@ namespace Nuclex.Support.Collections { /// Dictionary the multi dictionary will be based on internal MultiDictionary(IDictionary> dictionary) { this.typedDictionary = dictionary; +#if !WINRT this.objectDictionary = (this.typedDictionary as IDictionary); +#endif foreach(ICollection values in dictionary.Values) { this.count += values.Count; @@ -402,8 +411,10 @@ namespace Nuclex.Support.Collections { /// The wrapped Dictionary under its type-safe interface private IDictionary> typedDictionary; +#if !WINRT /// The wrapped Dictionary under its object interface private IDictionary objectDictionary; +#endif /// The number of items currently in the multi dictionary private int count; /// Provides the values stores in the dictionary in sequence diff --git a/Source/Collections/ObservableDictionary.cs b/Source/Collections/ObservableDictionary.cs index ca74a84..9824895 100644 --- a/Source/Collections/ObservableDictionary.cs +++ b/Source/Collections/ObservableDictionary.cs @@ -40,7 +40,11 @@ namespace Nuclex.Support.Collections { IDeserializationCallback, #endif IDictionary, +#if WINRT + ICollection, +#else IDictionary, +#endif #if !NO_SPECIALIZED_COLLECTIONS INotifyCollectionChanged, #endif @@ -329,6 +333,8 @@ namespace Nuclex.Support.Collections { #region IDictionary implementation +#if !WINRT + /// Adds an item into the Dictionary /// Key under which the item will be added /// Item that will be added @@ -344,12 +350,6 @@ namespace Nuclex.Support.Collections { return this.objectDictionary.Contains(key); } - /// Returns a new entry enumerator for the dictionary - /// The new entry enumerator - IDictionaryEnumerator IDictionary.GetEnumerator() { - return this.objectDictionary.GetEnumerator(); - } - /// Whether the size of the Dictionary is fixed bool IDictionary.IsFixedSize { get { return this.objectDictionary.IsFixedSize; } @@ -395,7 +395,23 @@ namespace Nuclex.Support.Collections { } } - #endregion +#endif // !WINRT + + #endregion // IDictionary implementation + + #region IDictionaryEnumerator implementation + +#if !WINRT + + /// Returns a new entry enumerator for the dictionary + /// The new entry enumerator + IDictionaryEnumerator IDictionary.GetEnumerator() { + return this.objectDictionary.GetEnumerator(); + } + +#endif // !WINRT + + #endregion // IDictionaryEnumerator implementation #region ICollection<> implementation @@ -477,8 +493,10 @@ namespace Nuclex.Support.Collections { /// The wrapped Dictionary under its type-safe interface private IDictionary typedDictionary; +#if !WINRT /// The wrapped Dictionary under its object interface private IDictionary objectDictionary; +#endif } diff --git a/Source/Collections/ReadOnlyDictionary.cs b/Source/Collections/ReadOnlyDictionary.cs index e9fe813..fa6f630 100644 --- a/Source/Collections/ReadOnlyDictionary.cs +++ b/Source/Collections/ReadOnlyDictionary.cs @@ -37,8 +37,12 @@ namespace Nuclex.Support.Collections { ISerializable, IDeserializationCallback, #endif - IDictionary, - IDictionary { +#if WINRT + ICollection, +#else + IDictionary, +#endif + IDictionary { #if !NO_SERIALIZATION @@ -233,6 +237,8 @@ namespace Nuclex.Support.Collections { #region IDictionary implementation +#if !WINRT + /// Removes all items from the Dictionary void IDictionary.Clear() { throw new NotSupportedException( @@ -256,12 +262,6 @@ namespace Nuclex.Support.Collections { return this.objectDictionary.Contains(key); } - /// Returns a new entry enumerator for the dictionary - /// The new entry enumerator - IDictionaryEnumerator IDictionary.GetEnumerator() { - return this.objectDictionary.GetEnumerator(); - } - /// Whether the size of the Dictionary is fixed bool IDictionary.IsFixedSize { get { return this.objectDictionary.IsFixedSize; } @@ -313,7 +313,23 @@ namespace Nuclex.Support.Collections { } } - #endregion +#endif // !WINRT + + #endregion // IDictionary implementation + + #region IDictionaryEnumerator implementation + +#if !WINRT + + /// Returns a new entry enumerator for the dictionary + /// The new entry enumerator + IDictionaryEnumerator IDictionary.GetEnumerator() { + return this.objectDictionary.GetEnumerator(); + } + +#endif // !WINRT + + #endregion // IDictionaryEnumerator implementation #region ICollection<> implementation @@ -394,8 +410,10 @@ namespace Nuclex.Support.Collections { /// The wrapped Dictionary under its type-safe interface private IDictionary typedDictionary; +#if !WINRT /// The wrapped Dictionary under its object interface private IDictionary objectDictionary; +#endif /// ReadOnly wrapper for the keys collection of the Dictionary private ReadOnlyCollection readonlyKeyCollection; /// ReadOnly wrapper for the values collection of the Dictionary diff --git a/Source/TypeHelper.cs b/Source/TypeHelper.cs index 6563d54..3e8f04d 100644 --- a/Source/TypeHelper.cs +++ b/Source/TypeHelper.cs @@ -58,7 +58,33 @@ namespace Nuclex.Support { #endregion // class MemberInfoComparer -#if !(XBOX360 || WINDOWS_PHONE) +#if WINRT + + /// + /// 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) { + var fieldInfoSet = new HashSet(fieldInfos, FieldInfoComparer.Default); + + while(type != typeof(object)) { + TypeInfo typeInfo = type.GetTypeInfo(); + + foreach(FieldInfo fieldInfo in typeInfo.DeclaredFields) { + fieldInfoSet.Add(fieldInfo); + } + + type = typeInfo.BaseType; + } + + FieldInfo[] fieldInfos = new FieldInfo[fieldInfoSet.Count]; + fieldInfoSet.CopyTo(fieldInfos, 0); + return fieldInfos; + } + +#elif !(XBOX360 || WINDOWS_PHONE) /// /// Returns all the fields of a type, including those defined in the type's base classes @@ -140,6 +166,23 @@ namespace Nuclex.Support { #endif // !(XBOX360 || WINDOWS_PHONE) +#if WINRT + + /// 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) { + foreach(ConstructorInfo constructorInfo in type.GetTypeInfo().DeclaredConstructors) { + if(constructorInfo.IsPublic && (constructorInfo.GetParameters().Length == 0)) { + return true; + } + } + + return false; + } + +#else + /// Determines whether the given type has a default constructor /// Type which is to be checked /// True if the type has a default constructor @@ -156,6 +199,8 @@ namespace Nuclex.Support { return false; } +#endif + /// Determines whether the type has the specified attribute /// Attribute the type will be checked for /// @@ -166,6 +211,20 @@ namespace Nuclex.Support { return type.HasAttribute(typeof(TAttribute)); } +#if WINRT + + /// Determines whether the type has the specified attribute + /// + /// Type that will be checked for presence of the specified attribute + /// + /// Attribute the type will be checked for + /// True if the type has the specified attribute, otherwise false + public static bool HasAttribute(this Type type, Type attributeType) { + return (type.GetTypeInfo().GetCustomAttribute(attributeType) != null); + } + +#else + /// Determines whether the type has the specified attribute /// /// Type that will be checked for presence of the specified attribute @@ -177,6 +236,8 @@ namespace Nuclex.Support { return (attributes != null) && (attributes.Length > 0); } +#endif + } } // namespace Nuclex.Support