Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to AddProperty to a C# class at runtime
Message
From
27/07/2007 22:07:32
 
 
To
27/07/2007 14:55:15
General information
Forum:
ASP.NET
Category:
Web Services
Miscellaneous
Thread ID:
01243387
Message ID:
01244303
Views:
31
Hi Gary

Thank you for your prompt reply.

Unfortunately, as I mentioned, the Web Service must return a Serializable object not an XML string. Your code just converts the Dictionary to an XML string and returns the string.

However could you please modify your code to Serialize the Dictionary since the web page expects a serializable object.

To tell you the truth, I could easily create an XML document for returning in a Foxpro Web Service but that will not do for this service. Therefore I need to return an object that NET will then Serialize.

Thanks for your help so far. This has brought me quite a long way with the Dictionary object.

Thanks

Bernard


>Hi Bernard
>
>Your message refers.
>
>Here is some revised code. This is the same as before except, I have converted the Dictionary to an XML Document. The last line writes the XML Document as a string with which you can use in your webservice.
>
>I could have serialized the dictionary by creating a class that implemented ISerializable but, this method is easier and probably more understandable for you at this stage.
>
>Doubtless you will be able to handle the XML string at the other end.
>
>HTH
>
>-=Gary
>
>
>using System;
>using System.Collections.Generic;
>using System.Text;
>using System.IO;
>using System.Xml;
>
>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();
>
>            // Create a new XML document.
>            XmlDocument doc = new XmlDocument();
>
>            // Add a declaration (if you want to)
>            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "", "");
>
>            // Add the declaration to the XML document (if you want to)
>            doc.AppendChild(dec);
>
>            // Create a root node called "Properties".
>            XmlNode rootNode = doc.CreateElement("Properties");
>
>            // Iterate around the dictionary and create an XML document.
>            foreach (KeyValuePair<string, object> pair in properties)
>            {
>                // Create an Xml node that should be a child of the root node.
>                XmlNode property = doc.CreateElement("Property");
>
>                // Create an attribute called "Name".
>                XmlAttribute attrib = doc.CreateAttribute("Name");
>
>                // Specify the value of this attribute to be the key value of the dictionary entry.
>                attrib.Value = pair.Key;
>
>                // Add the new attribute to the attribute collection of the property node.
>                property.Attributes.Append(attrib);
>
>                // Assign the value of the dictionary entry to the InnerText value of the property node.
>                property.InnerText = pair.Value.ToString();
>
>                // Add the property node as a child of the rootNode.
>                rootNode.AppendChild(property);
>            }
>
>            // We have stopped iterating over the dictionary so, add the rootNode to the XmlDocument.
>            doc.AppendChild(rootNode);
>
>            // Finally, write the XML document out as a string to the xmlData string variable.
>            string xmlData = doc.InnerXml;
>        }
>    }
>
>    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