1
[CollectionDataContract(Namespace = "CISICPD")]
[KnownType(typeof(List<CISICPD.LeeDictionary>))]
public class LeeDictionary : Dictionary<string, object> 
{
}

[DataContract(Namespace = "CISICPD")]
[KnownType(typeof(List<CISICPD.LeeDictionary>))]
public class TestResponse
{
    [DataMember]
    public List<LeeDictionary> Results;

    public TestResponse() { Results = new List<LeeDictionary>(); }
    [OnDeserializing]
    private void OnDeserialize(StreamingContext c) { Results = new List<LeeDictionary>(); }
}

如果在服务引用中使用上述内容,则生成reference.cs的将忽略上面指定的 knowntypes。

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="TestResponse", Namespace="CISICPD")]
[System.SerializableAttribute()]
public partial class TestResponse : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {

我指定了,LeeDictionary所以我可以设置我自己的命名空间(认为它可能是命名空间问题),将其更改为Dictionary<string,object>是一样的。当字典本身包含另一个字典并抱怨类型未知时,问题就来了。

如果我将 knowntype 行添加到reference.cs它,那么一切正常,但我不明白为什么它不会在生成引用时将其放入?

4

0 回答 0