Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to AddProperty to a C# class at runtime
Message
From
29/07/2007 06:20:30
 
 
To
29/07/2007 03:31:59
General information
Forum:
ASP.NET
Category:
Web Services
Miscellaneous
Thread ID:
01243387
Message ID:
01244414
Views:
25
Hi Gary

Thank you for your reply. I ran your console application and it worked as it should. However when I try it from a Web Service this is all I got:
<?xml version="1.0" encoding="utf-8" ?> 
  <PropertyWrapper xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/" /> 
As you can see no properties and values.

>Over here, I don't have your web service.

This is what I did:
1. Created a new Web Service.
2. Added a class - Class2.cs and put the following code in it which I copied and pasted from your code sample:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace SampleService
{
    [Serializable()]
    public class PropertyWrapper : ISerializable
    {
        #region " Private Members "
        [NonSerialized]
        private Dictionary<string, object> _runtTimeProperties = new Dictionary<string, object>();
        #endregion
        #region " Ctors "
        /// <summary>
        /// Default constuctor.
        /// </summary>
        public PropertyWrapper()
        { }
        /// <summary>
        /// This constructor is special and required for deserialization.
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        protected PropertyWrapper(SerializationInfo info, StreamingContext context)
        {
            // Enumerate through SerializationInfo and rebuild the internal dictionary.
            SerializationInfoEnumerator e = info.GetEnumerator();
            while (e.MoveNext())
            {
                _runtTimeProperties.Add(e.Name, e.Value);
            }
        }
        #endregion
        #region " Public Members "
        #region " AddProperty(string vfpStylePropertyName, object vfpStylePropertyValue) "
        /// <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 vfpStylePropertyValue)
        {
            try
            {
                _runtTimeProperties.Add(vfpStylePropertyName, vfpStylePropertyValue);
            }
            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
        #endregion
        #region " ISerializable Members "
        #region " GetObjectData(SerializationInfo info, StreamingContext context) "
        /// <summary>
        /// Add data into the serialization stream.
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            foreach (KeyValuePair<string, object> pair in _runtTimeProperties)
            {
                info.AddValue(pair.Key, pair.Value);
            }
        }
        #endregion
        #endregion
    }
}
3. I edited the service.cs file to look like this:
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
namespace SampleService
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Service : System.Web.Services.WebService
    {
        [WebMethod]
        public PropertyWrapper GetInfo()
        {
            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");
            return wrapper;
        }
    }
}
4. I edited the service.asmx file to add my namespace:
<%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Class="SampleService.Service" %>
The other files I did not change. I compiled and ran and tested the Web Service. As I showed above I did not get any properties nor values. What am I doing wrong? You now have my web service sample code so please help. How is it that your console class can Serialize/DeSerialize properly while my above Web Service does not?

Thanks for your patience and time so far.

Bernard


>Bernard,
>
>You are taking the demo code too seriously.
>
>The XML code you refer to from the sample has nothing to do with anything - I just left it in for good measure (effectively to show you how to build an XML document in .NET and load it from a generic dictionary - a tutorial, for want of a better expression).
>
>>2. The other code uses a File based stream and Deserializes it. Again I don't want to have anything file based.
>
>This code, again, is demo code only. It is a way for you to test the class and prove to yourself that it will serialize and then deserialize, without having to rig it up in a web service. Over here, I don't have your web service. So, to ensure that the new class works, the simplest way to test it is to serialize it to/from a simple disk based file.
>
>All you need is code similar to the following in your web method :-
>
>
>            ...
>
>            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");
>            return wrapper;
>
>
>
>If you decide to run the test to and from the file, you can satisfy yourself that the class is in fact serializable. Thereafter, treat it as any other serializable .NET class in so far as your web method is concerned.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform