/[xmltv_parser]/trunk/libxmltv/Core/PropertyDictionary.cs
ViewVC logotype

Diff of /trunk/libxmltv/Core/PropertyDictionary.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/libxmltv/Core/PropertyDictionary.cs	2013/03/10 09:01:33	95
+++ trunk/libxmltv/Core/PropertyDictionary.cs	2013/03/10 14:28:57	110
@@ -10,14 +10,27 @@
 
 namespace libxmltv.Core
 {
-    
+    [Serializable]
     public class PropertyCollection<T> : IPropertyCollection<T>
     {
         private List<T> items = new List<T>();
         public PropertyCollection() : this(new List<T>()) { }
         public PropertyCollection(ICollection<T> collection) { foreach (var t in collection) { items.Add(t); } }
         public int PropertyCount { get { return items.Count; } }
-        public bool IsReadOnly { get { return (items as ICollection<T>).IsReadOnly; } }
+
+        public bool IsReadOnly
+        {
+            get
+            {
+                ICollection<T> collection = (items as ICollection<T>);
+                if (collection == null)
+                {
+                    throw new InvalidCastException(string.Format("Unable to cast: '{0}' to '{1}'", items.GetType().Name, typeof(ICollection<T>).Name));
+                }
+                return collection.IsReadOnly;
+            }
+        }
+
         public void AddProperty(T item) { items.Add(item); }
         public void ClearProperties() { items.Clear(); }
         public bool ContainsProperty(T item) { return items.Contains(item); }
@@ -25,7 +38,13 @@
         public bool RemoveProperty(T item) { return items.Remove(item); }
         public IEnumerator<T> GetEnumerator() { return items.GetEnumerator(); }
         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return items.GetEnumerator(); }
+
+        public override string ToString()
+        {
+            return string.Format("Property Count: {0}", PropertyCount);
+        }
     }
+    [Serializable]
     public class PropertyDictionary : PropertyDictionary<string, object>, IPropertyDictionary
     {
         public PropertyDictionary() : base() { }
@@ -36,6 +55,7 @@
         public PropertyDictionary(int capacity, IEqualityComparer<string> comparer) : base(capacity, comparer) { }
         protected PropertyDictionary(SerializationInfo info, StreamingContext context) : base(info, context) { }
     }
+    [Serializable]
     public class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue>
     {
         [StructLayout(LayoutKind.Sequential)]
@@ -45,6 +65,10 @@
             public int next;
             public TKey name;
             public TValue value;
+            public override string ToString()
+            {
+                return new PropertyValuePair<TKey, TValue>(name, value).ToString();
+            }
         }
         private Entry[] entries
         {
@@ -116,25 +140,40 @@
         public bool RemoveProperty(TKey key) { return properties.Remove(key); }
         public bool TryGetPropertyValue(TKey key, out TValue value) { return properties.TryGetValue(key, out value); }
         public int PropertyCount { get { return properties.Count; } }
-        public bool IsReadOnly { get { return (properties as IPropertyCollection<TKey>).IsReadOnly; } }
-        public void AddProperty(PropertyValuePair<TKey, TValue> item) { AddProperty(item.Name, item.Value); }
+        public bool IsReadOnly
+        {
+            get
+            {
+                IDictionary<TKey, TValue> collection = (properties as IDictionary<TKey, TValue>);
+                if (collection == null)
+                {
+                    throw new InvalidCastException(string.Format("Unable to cast: '{0}' to '{1}'", properties.GetType().Name, typeof(IDictionary<TKey, TValue>).Name));
+                }
+                return collection.IsReadOnly;
+            }
+        }
+        public void AddProperty(IPropertyValuePair<TKey, TValue> item) { AddProperty(item.Name, item.Value); }
         public void ClearProperties() { properties.Clear(); }
-        public bool ContainsProperty(PropertyValuePair<TKey, TValue> item) { return ContainsProperty(item.Name); }
-        public void CopyPropertiesTo(PropertyValuePair<TKey, TValue>[] array, int arrayIndex) 
+        public bool ContainsProperty(IPropertyValuePair<TKey, TValue> item) { return ContainsProperty(item.Name); }
+        public void CopyPropertiesTo(IPropertyValuePair<TKey, TValue>[] array, int arrayIndex) 
         {
-            var list = properties.ToList().Cast<PropertyValuePair<TKey, TValue>>().ToList();
+            var list = properties.ToList().Cast<IPropertyValuePair<TKey, TValue>>().ToList();
             list.CopyTo(array, arrayIndex); 
         }
-        public bool RemoveProperty(PropertyValuePair<TKey, TValue> item) { return RemoveProperty(item.Name); }
-        IEnumerator<PropertyValuePair<TKey, TValue>> IEnumerable<PropertyValuePair<TKey, TValue>>.GetEnumerator() { return new PropertyDictionaryEnumerator(this, 2); }
+        public bool RemoveProperty(IPropertyValuePair<TKey, TValue> item) { return RemoveProperty(item.Name); }
+        IEnumerator<IPropertyValuePair<TKey, TValue>> IEnumerable<IPropertyValuePair<TKey, TValue>>.GetEnumerator() { return new PropertyDictionaryEnumerator(this, 2); }
         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return properties.GetEnumerator(); }
 
+        public override string ToString()
+        {
+            return string.Format("Property Count: {0}", PropertyCount);
+        }
         #region enumerator support
-        public class PropertyDictionaryEnumerator: IEnumerator<PropertyValuePair<TKey,TValue>>, IDisposable
+        public class PropertyDictionaryEnumerator : IEnumerator<IPropertyValuePair<TKey, TValue>>, IDisposable
         {
             private PropertyDictionary<TKey, TValue> dictionary;
             private int index;
-            private PropertyValuePair<TKey, TValue> current;
+            private IPropertyValuePair<TKey, TValue> current;
             private int getEnumeratorRetType;
             internal const int DictEntry = 1;
             internal const int KeyValuePair = 2;
@@ -148,7 +187,7 @@
                 this.current = new PropertyValuePair<TKey, TValue>();
             }
 
-            public PropertyValuePair<TKey, TValue> Current
+            public IPropertyValuePair<TKey, TValue> Current
             {
                 get { return this.current; }
             }
@@ -183,7 +222,7 @@
                 {
                     if (this.dictionary.entries[this.index].hashCode >= 0)
                     {
-                        this.current = new KeyValuePair<TKey, TValue>(this.dictionary.entries[this.index].name, this.dictionary.entries[this.index].value);
+                        this.current =(IPropertyValuePair<TKey,TValue>)(PropertyValuePair<TKey,TValue>)new KeyValuePair<TKey, TValue>(this.dictionary.entries[this.index].name, this.dictionary.entries[this.index].value);
                         this.index++;
                         return true;
                     }
@@ -200,7 +239,7 @@
                     ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
                 }
                 this.index = 0;
-                this.current = new KeyValuePair<TKey, TValue>();
+                this.current = (IPropertyValuePair<TKey, TValue>)(PropertyValuePair<TKey, TValue>)new KeyValuePair<TKey, TValue>();
             }
         }
         #endregion   

 

  ViewVC Help
Powered by ViewVC 1.1.22