0

我有一个 API 可以返回多个我事先不知道的东西,所以我必须把它作为 return type System.Object

我对某些类型有一些问题。就像当它返回不同的对象时它很好但是当它返回时它System.Int32不起作用并告诉我:

Cannot serialize parameter of type 'System.Int32' because it is not the exact type 'System.Object' in the method signature and is not in the known types collection.

任何想法 ?

我知道我们可以使用 ServiceKnownType 但我想知道是否有其他方法可以做到这一点?我需要这个...中的所有现有类型(int、bool、string 等)。

4

2 回答 2

1

不要在服务合同上返回 System.Object 类型,即使它是 WCF 4 REST。默认序列化程序/格式化程序如何为您的合同产生响应。

您可以返回字节数组,将您的 System.Object 转换为字符串并返回它。无论如何,您始终可以将对象序列化为 XML 或 JSON 并以字符串形式返回。

还可以链接到具有相同问题的论坛,也许它可以帮助您: http ://social.msdn.microsoft.com/Forums/nl-NL/wcf/thread/1f43e9b4-39a4-481b-88f0-18c7d4416745

于 2012-06-18T21:57:00.993 回答
0

您可以创建一个简单的 if 语句来检查它是否为 int,然后您可以将其转换为对象。顺便说一句,整数不能成为对象,直到它们是一个字符串,所以如果我这样做,我会做一个简单的:

blah.toString();

并且 bam 你现在可以将它用作对象。

于 2012-06-18T21:50:41.263 回答