Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to AddProperty to a C# class at runtime
Message
From
26/07/2007 23:48:54
 
 
To
26/07/2007 06:01:14
General information
Forum:
ASP.NET
Category:
Web Services
Miscellaneous
Thread ID:
01243387
Message ID:
01244012
Views:
28
Hi Gary

Thanks a million for the effort and the code sample. I will examine it over the weekend and see if I can understand what is going on. Only then will I be able to adapt it for my needs.

Thanks again.

>Hi Bernard,
>
>I'm not totally sure what you are trying to do but, I have written a little code below that you can paste straight into a C# Console application. What it does is as follows:-
>
>1) Creates an instance of a PropertyWrapper class.
>2) Allows you to call the AddProperty() method to add, well, properties :)
>3) Shows you how to extract indvidual property values and how to test for properties that may not exist.
>4) Allows you to get the property collection (in the form of a generic dictionary) out of the PropertyWrapper class.
>5) Shows you how to iterate through the dictionary extracting the property name and its value
>
>Finally, the Dictionary class is serializable so, you should have no problem returning the collection from a web service.
>
>HTH
>
>Best,
>
>-=Gary
>
>
>
>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
>    }
>}
>
>
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform