0
votes

I am calling a SalesForce API added as a WebReference in my Console Test application.

one of the parameter that it requires is of type object. To be precise, following is my code:

 SFObject sfObject = new SFObject
            {
                type = "User",
                Item = new { ExternalId = 2}
            };

I am passing the code above where API is expecting the Item's type to be object().

When I make the final call, I am seeing following error:

{"<>f__AnonymousType0`1[System.Int32] cannot be serialized

Below is definition of SFObject as my "Add web reference" downloaded it.

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.81.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:sfobject.sfapi.successfactors.com")]
    public partial class SFObject {

        private object itemField;

        private string typeField;

        private System.Xml.XmlElement[] anyField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("businessKeys", typeof(BusinessKeys))]
        [System.Xml.Serialization.XmlElementAttribute("id", typeof(string))]
        public object Item {
            get {
                return this.itemField;
            }
            set {
                this.itemField = value;
            }
        }

        /// <remarks/>
        public string type {
            get {
                return this.typeField;
            }
            set {
                this.typeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAnyElementAttribute()]
        public System.Xml.XmlElement[] Any {
            get {
                return this.anyField;
            }
            set {
                this.anyField = value;
            }
        }
    }

I searched around and seems like there is some problem with WCF serialization but I am not using WCf here. Is there any way that I can get around this problem?

1
Please metion SFObject class and this properties and all of things that relevance to it.vahid kargar
@vahidkargar: just added the infoLost
What's not clear about the error? Anonymous types can't be serialized.Daniel Mann

1 Answers

1
votes

Well the short answer for this lies in the folloing piece of the code:

[System.Xml.Serialization.XmlElementAttribute("businessKeys", typeof(BusinessKeys))]
        [System.Xml.Serialization.XmlElementAttribute("id", typeof(string))]
        public object Item {
            get {
                return this.itemField;
            }
            set {
                this.itemField = value;
            }
        }

where

[System.Xml.Serialization.XmlElementAttribute("businessKeys", typeof(BusinessKeys))]
            [System.Xml.Serialization.XmlElementAttribute("id", typeof(string))]

part of it demands it to be either of Type String or of Type BusinessKeys. If anything else is sent over then it will get rejected as unexpected type. If you try to fool the system with new{} keyword then it will throw the error that it just did throw.