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 00:56:08
 
 
To
28/07/2007 07:01:00
General information
Forum:
ASP.NET
Category:
Web Services
Miscellaneous
Thread ID:
01243387
Message ID:
01244405
Views:
26
Hi Gary

I have had a look over the code. Please correct me if I am wrong.

1. // Create a new XML document.
XmlDocument doc = new XmlDocument();
Here you are creating a XML document as a string. Basically converting the Dictionary and its values to a XML string. If I return this from my web service, I will be returning it as a string not as an object that NET will Serialize. Therefore at the calling side, the program will have to Deserialize it to use it. This means that there will be no intellisense at that end.

2. The other code uses a File based stream and Deserializes it. Again I don't want to have anything file based.

My Class code I have now that works :
public class MemberDetail
    {
        public string csName = "";
        public string csCompany = "";
        public string csAddress = "";
        public DateTime tsEntered = DateTime.Now;
    }  
    public class CreditCard
    {
        public string csName = "";
        public string csNumber = "";
        public string csExpiry = "";
    }
    public class ResponseObject // this is returned from the Web Method
    {
        public int ErrorCode = 0;
        public string ErrorText = "";
        public string SessionID = "";
        // I want to avoid "hard coding this at design time, hence a Dictionary"
        public CreditCard CCard = new CreditCard(); 
        public MemberDetail MDetail = new MemberDetail();
and the Web Method:
        [WebMethod]
        // here we are returning a Serializable object not a string.
        public ResponseObject GetResp()
        {
            // create the CreditCard object and fill it with values
            CreditCard cc = new CreditCard();
            cc.csExpiry = "22/11/2007";
            cc.csName = "Gary Wynne";
            cc.csNumber = "2233 3242 3323 1211";
            // create the MemberDetail object and fill it with values
            MemberDetail mo = new MemberDetail();
            mo.csAddress = "23 Fleet Street";
            mo.csCompany = "Newspapers Corp";
            mo.csName = "John Smith";
           // create the ResponseObject object and fill it with values
            ResponseObject ro = new ResponseObject();
            ro.ErrorCode = 0;
            ro.ErrorText = "No Error Here";
            ro.SessionID = "1234abcd";
            ro.MDetail = mo;
            ro.CCard = cc;
            return ro;
        }
This gives me output as I need it:
<?xml version="1.0" encoding="utf-8" ?> 
- <ResponseObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://localhost/">
  <ErrorCode>0</ErrorCode> 
  <ErrorText>No Error Here</ErrorText> 
  <SessionID>1234abcd</SessionID> 
- <CCard>
  <csName>Gary Wynne</csName> 
  <csNumber>2233 3242 3323 1211</csNumber> 
  <csExpiry>22/11/2007</csExpiry> 
  </CCard>
- <MDetail>
  <csName>John Smith</csName> 
  <csCompany>Newspapers Corp</csCompany> 
  <csAddress>23 Fleet Street</csAddress> 
  <tsEntered>2007-07-29T14:42:40.484375+10:00</tsEntered> 
  </MDetail>
  </ResponseObject>
So is this possible using a Dictionary, where I can do code like this below?
[WebMethod]
        public object GetMember()
        {
            // create the main class for adding props
            propertywrapper wrapper = new propertywrapper();
           // Step 1 - call the AddProperty() to add the 3 main objects
           // ResponseObject is always returned, the others if needed.
            ResponseObject ro = new ResponseObject();
            wrapper.Addproperty("ResponseObject", ro);
           //wrapper.Addproperty("ErrorTExt", "");
           //wrapper.Addproperty("SessionID", "");
           // Step 2 - instantiate the MemberDetails class if needed
            MemberDetail mdetail = new MemberDetail();
            wrapper.Addproperty("MemberDetail", mdetail);
           // Step 3 - instantiate and add the ccard class if needed
           // CreditCard cc = new CreditCard();
           // wrapper.Addproperty("CredirCard", cc);

            // return the dictionary values
            Dictionary<string, object> fullobject = wrapper.GetProps();
            return fullobject;
        }
and return an Object rather than a string/xmlstring.

The reason I took up doing the Web Service in NET, is because I needed to return an object. I can easily return a XML string from VFP so returning a string or XML String is not an option here. I need to be able to return an object as I showed in my code above.

Thanks for your help

Bernard
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform