> >using System; >using System.Collections.Generic; >using System.Text; > >namespace ConsoleApplication1 >{ > public class Bernard > { > public static void Main() > { > // Create an instance of our custom Property Wrapper class. > PropertyWrapper wrapper = new PropertyWrapper(); > > // Add a few properties to it. > wrapper.AddProperty("Name", "Bernard Bout"); > wrapper.AddProperty("DOB", Convert.ToDateTime("01/01/1970")); > wrapper.AddProperty("Phone", "+44 161 777 5555"); > wrapper.AddProperty("Email", "bernard@someplace.com"); > > // Get some values we know exist. > string nameValue = wrapper.GetValue("Name").ToString(); > DateTime dobValue = Convert.ToDateTime(wrapper.GetValue("DOB")); > > // Output these values to the console. > Console.WriteLine("\"Name\" == " + nameValue); > Console.WriteLine("\"DOB\" == " + dobValue.ToString()); > > // Try for one that doesn't. > object badValue = wrapper.GetValue("BadValue"); > if (badValue == null) > { > Console.WriteLine("*** \"BadValue\" doesn't exist ***"); > } > > Console.WriteLine(); > Console.WriteLine(); > > // Get hold of the generic dictionary. > Dictionary<string, object> properties = wrapper.GetProperties(); > > // Iterate around the dictionary and ouput the key/value pairs to the console window. > foreach (KeyValuePair<string, object> pair in properties) > { > Console.WriteLine(string.Format("The property name is \"{0}\" and it's value is: {1}", pair.Key, pair.Value)); > } > > Console.Read(); > } > } > > public class PropertyWrapper > { > #region " Private Member Fields " > > // Create a private member field called _runtTimeProperties that is of type > // generic dictionary where the "key" is based on a string type and the > // value is based on an object type. This means that you can add any > // value to the dictionary because everything is derived from object. > private Dictionary<string, object> _runtTimeProperties = new Dictionary<string, object>(); > > #endregion > > #region " AddProperty(string vfpStylePropertyName, object vfpStylePropertValue) " > /// <summary> > /// This method is a simple wrapper that allows you to add a property/value > /// to the internal dictionary as a key/value pair. > /// </summary> > /// <param name="vfpStylePropertyName"></param> > /// <param name="vfpStylePropertValue"></param> > public void AddProperty(string vfpStylePropertyName, object vfpStylePropertValue) > { > try > { > _runtTimeProperties.Add(vfpStylePropertyName, vfpStylePropertValue); > } > catch (Exception e) > { > // Handle duplicate key added to Dictionary. > throw new Exception("Error occured adding property", e); > } > } > #endregion > > #region " GetProperties() " > /// <summary> > /// Return the dictionary. > /// </summary> > /// <returns></returns> > public Dictionary<string, object> GetProperties() > { > return _runtTimeProperties; > } > #endregion > > #region " GetValue(string propertyName) " > /// <summary> > /// Get an individual property value. > /// </summary> > /// <param name="propertyName"></param> > /// <returns></returns> > public object GetValue(string propertyName) > { > object outValue = null; > > // If the propertyName cannot be found in _runtTimeProperties, TryGetValue() > // returns false which on this occasion can be discarded. If the value isn't > // found, outValue stays null > _runtTimeProperties.TryGetValue(propertyName, out outValue); > return outValue; > } > #endregion > } >} > >