无法序列化,因为它没有无参数构造函数

cannot be serialized because it does not have a parameterless constructor

我正在调用在我的控制台测试应用程序中添加为 WebReference 的 SalesForce API。

它需要的参数之一是对象类型。准确地说,以下是我的代码:

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

我传递上面的代码,其中 API 期望项目的类型是 object()。

当我进行最后一次调用时,我看到以下错误:

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

下面是 SFObject 的定义,因为我 "Add web reference" 下载了它。

[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;
            }
        }
    }

我四处搜索,似乎 WCF 序列化存在一些问题,但我在这里没有使用 WCf。有什么办法可以解决这个问题吗?

这个问题的简短答案就在下面的代码中:

[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;
            }
        }

哪里

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

它的一部分要求它是类型 String 或类型 BusinessKeys。如果发送了任何其他内容,它将作为意外类型被拒绝。如果您尝试使用 new{} 关键字来欺骗系统,那么它会抛出它刚刚抛出的错误。