[Bobbot-cvs] Bob/Core/Collections PropertyCollection.cs,NONE,1.1
Status: Alpha
Brought to you by:
iainmckay
From: Iain M. <iai...@us...> - 2004-12-30 03:33:38
|
Update of /cvsroot/bobbot/Bob/Core/Collections In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8381/Core/Collections Added Files: PropertyCollection.cs Log Message: Many new changes. Most notably host-based recognition. --- NEW FILE: PropertyCollection.cs --- #region LGPL License /* Bastard of a Bot Library Copyright (C) 2004 Bastard of a Bot Team This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 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 GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #endregion using System; using System.Collections; using Bot.Core; namespace Bot.Core.Collections { public class PropertyCollection { #region Public properties public PropertyEntry this[string name] { get { return (m_Table[name] as PropertyEntry); } } public ICollection Keys { get { return m_Table.Keys; } } public ICollection Values { get { return m_Table.Values; } } public int Count { get { return m_Table.Count; } } #endregion #region Private members private Hashtable m_Table; #endregion #region Constructor public PropertyCollection() { m_Table = new Hashtable(); } #endregion #region Public methods public void AddProperty(string name, object value, Type type) { if(name == null) throw new ArgumentNullException("name", "name cannot be null"); if(type == null) throw new ArgumentNullException("type", "type cannot be null"); try { m_Table[name] = new PropertyEntry(name, ChangeType(value, type), type); } catch(FormatException ex) { throw new FormatException(ex.Message, ex); } } public void RemoveProperty(string name) { m_Table.Remove(name); } public bool ContainsProperty(string name) { return (m_Table[name] != null); } public void SetProperty(string name, object value) { PropertyEntry toSet = this[name]; if(toSet == null) return; try { toSet.Value = ChangeType(value, toSet.Type); } catch(FormatException ex) { throw new FormatException(ex.Message, ex); } } public void Clear() { m_Table.Clear(); } public IEnumerator GetEnumerator() { return new Enumerator(this); } #endregion #region Private methods private object ChangeType(object value, Type type) { try { return Convert.ChangeType(value, type); } catch(FormatException ex) { throw new FormatException(ex.Message, ex); } } #endregion #region Enumerator public sealed class Enumerator : IEnumerator { #region Private members private IDictionaryEnumerator m_InternalEnum; #endregion #region Constructor internal Enumerator(PropertyCollection target) { m_InternalEnum = target.m_Table.GetEnumerator(); } #endregion #region IEnumerator implementation public object Current { get { return (PropertyEntry) m_InternalEnum.Value; } } public void Reset() { m_InternalEnum.Reset(); } public bool MoveNext() { return m_InternalEnum.MoveNext(); } #endregion } #endregion } } |