WCF 序列化複雜物件時,提示未知型別錯誤

2021-08-31 02:06:37 字數 620 閱讀 5707

問題描述:

system.reflection.targetinvocationexception: 呼叫的目標發生了異常。

system.invalidoperationexception: 生成 xml 文件時出錯。

使用 xmlinclude 或 soapinclude 屬性靜態指定非已知的型別。

解決方案:

在類宣告處增加「xmlinclude」和「knowntype」兩個特性。

例如表a中包括「a1」和「a2」兩個自定義物件,**如下:

///

/// 表a

///

[datacontract]

[knowntype(typeof(a1))]

[xmlinclude(typeof(a1))]

[knowntype(typeof(a2))]

[xmlinclude(typeof(a2))]

public class class1

///

/// 欄位a2

///

[datamember]

public arraylist a2 }

物件序列化 反序列化

必須新增引用 using system.io using system.runtime.serialization using system.runtime.serialization.formatters.binary 方法 region 物件序列化 物件序列化 任意物件 字串 public st...

物件 序列化 反序列化

public class eventmessage 資訊提示類 型別1 操作日誌2 安全日誌 標題 內容 icon型別 url 執行script指令碼字串 需加 public static void messagebox int m type,string m title,string m body...

物件序列化

using system using system.io using system.diagnostics using system.reflection using system.runtime.serialization using system.runtime.serialization.fo...