繁体   English   中英

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

[英]cannot be serialized because it does not have a parameterless constructor

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

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

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

我通过上面的代码,其中API期望Item的类型为object()。

当我进行最后通话时,看到以下错误:

{“ <> f__AnonymousType0`1 [System.Int32]无法序列化

下面是SFObject的定义,因为我下载了它的“添加Web参考”

[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 {}关键字欺骗系统,那么它将抛出刚刚抛出的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM