1

dictionary <string, object>オブジェクトに int、bool などの基本型が含まれる可能性がある場所、または別の配列が含まれる可能性がある場所の配列を返そうとしています。dictionary<string, object>

正常にシリアライズできますが、ディクショナリ内にディクショナリがある場合、デシリアライズされません。

次のエラーが表示されます。

Error in line 1 position 543. Element 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:Value' contains data from a type that maps to the name 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:ArrayOfArrayOfKeyValueOfstringanyType'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'ArrayOfArrayOfKeyValueOfstringanyType' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.

クラス:

[DataContract(Namespace = "CISICPD")]
[KnownType(typeof(Dictionary<string,object>))]
public class TestResponse
{
    [DataMember]
    public Dictionary<string,object>[] Results;
}

関数:

public TestResponse test(string test1, string test2)
    {
        TestResponse r = new TestResponse();
        r.Results = new Dictionary<string, object>[1];
        r.Results[0] = new Dictionary<string, object>();
        r.Results[0].Add("field1", 26);
        Dictionary<string, object>[] d = new Dictionary<string, object>[1];
        d[0] = new Dictionary<string, object>();
        d[0].Add("inner", 28);
        r.Results[0].Add("dictionary", d);
        return r;
    }

これを実行するとエラーメッセージが表示されますが、正しい既知のタイプを取得したと思いますか?

CISICPD.CPDClient t = new CISICPD.CPDClient();
CISICPD.TestResponse response = t.test("dgdf", "dfsdfd");
4

2 に答える 2

1

次のプロパティを datacontract クラスに追加するだけです。

[DataMember]
public object UsedForKnownTypeSerializationObject;

したがって、生成されたプロキシには、データコントラクトで設定した Knowtypes が含まれています。私は同じ問題を抱えていましたが、これが私が思いついた唯一の解決策です。Object 型のプロパティを DataContract クラスに指定しない場合、生成されたプロキシには宣言された knowtypes が含まれません。

例えば:

[DataContract]
[KnownType(typeof(List<String>))]
public class Foo
{
    [DataMember]
    public String FooName { get; set; }

    [DataMember]
    public IDictionary<String, Object> Inputs { get; set; }

    [DataMember]
    private Object UsedForKnownTypeSerializationObject{ get; set; }

}

機能的な実装を持たないダミーのプロパティになってしまうため、それほどきれいではありません。しかし、再び私は別の解決策を持っていません。

于 2013-08-20T15:44:57.363 に答える
0

これを TestResponse の既知の型に追加します。

[KnownType(typeof(Dictionary<string, object>[]))]

"d" はテスト メソッドの Dictionary オブジェクトの配列であり、結果に値として格納しているため、既知の型に型を追加する必要があります。

詳細については、このリンクの「コレクションと既知の型」セクションを確認してください: http://msdn.microsoft.com/en-us/library/aa347850.aspx

基本的に、Results にオブジェクトとして格納するタイプには、KnownType を追加する必要があります。

于 2012-09-18T16:13:13.820 に答える