2015-09-25 32 views
0

我在我的控制檯測試應用程序中調用作爲WebReference添加的SalesForce API。無法序列化,因爲它沒有無參數的構造函數

它需要的參數之一是類型對象。準確地說,以下是我的代碼:

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

我傳遞上述其中API期待的項目的類型爲對象的代碼()。

當我作出最後的通話,我看到以下錯誤:

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

下面是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。有什麼方法可以解決這個問題嗎?

+0

請注意SFObject類和這個屬性以及與它相關的所有東西。 –

+0

@vahidkargar:剛剛添加了信息 – TeaLeave

+0

什麼是不明確的錯誤?匿名類型不能被序列化。 –

回答

1

那麼這個簡短的答案在於folloing一塊代碼:

[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))] 

部分要求它是兩種類型的字符串或類型的BusinessKeys。如果有其他內容被髮送過來,則會被拒絕爲意外類型。如果您嘗試用新的{}關鍵字欺騙系統,則會拋出它剛剛拋出的錯誤。

相關問題