为什么在序列化某些用户定义的类时会丢失数据?

时间:2010-10-29 14:05:34

标签: c# wcf

我有4节课: BaseStreamParameter

[DataContract]
[KnownType(typeof(StreamParameter))]
[KnownType(typeof(RepeatableStreamParameter))]
public class BaseStreamParameter
{
    private bool pIsRepeatable;
    [DataMember]
    public bool IsRepeatable
    {
        get { return pIsRepeatable; }
        set { pIsRepeatable = value; }
    }
    private bool pIsFBlock;
    [DataMember]
    public bool IsFBlock
    {
        get { return pIsFBlock; }
        set { pIsFBlock = value; }
    }

    private string pParamName;
    [DataMember]
    public string ParamName
    {
        get { return pParamName; }
        set { pParamName = value; }
    }


    public BaseStreamParameter(bool aIsRepeatable, bool aIsFBlock, string aParamName)
    {
        pIsFBlock = aIsFBlock;
        pIsRepeatable = aIsRepeatable;
        pParamName = aParamName;
    }

    public BaseStreamParameter(string aParamName)
    {
        pIsFBlock = false;
        pIsRepeatable = false;
        pParamName = aParamName;
    }

RepeatableStreamParameter

[DataContract]
public class RepeatableStreamParameter : BaseStreamParameter
{
    private List<BaseStreamParameter> pRepeatableParameters;
    [DataMember]
    public List<BaseStreamParameter> RepeatableParameters
    {
        get { return pRepeatableParameters; }
        set { pRepeatableParameters = value; }
    }

    public RepeatableStreamParameter(bool aIsFBlock, string aParamName)
        : base(true, aIsFBlock, aParamName)
    {
        pRepeatableParameters = new List<BaseStreamParameter>();
    }
}

StreamParameter

[DataContract]
public class StreamParameter : BaseStreamParameter
{
    private string pParamType;
    [DataMember]
    public string ParamType
    {
        get { return pParamType; }
        set { pParamType = value; }
    }

    private string pParamValue;
    [DataMember]
    public string ParamValue
    {
        get { return pParamValue; }
        set { pParamValue = value; }
    }
    private bool pIsEnum;
    [DataMember]
    public bool IsEnum
    {
        get { return pIsEnum; }
        set { pIsEnum = value; }
    }
    private List<string> pEnumList;
    [DataMember]
    public List<string> EnumList
    {
        get { return pEnumList; }
        set { pEnumList = value; }
    }

    public StreamParameter(bool aIsFBlock, string aParamName, string aParamType, string aParamValue, bool aIsEnum, List<string> aEnumList)
        : base(false, aIsFBlock, aParamName)
    {
        pParamType = aParamType;
        pParamValue = aParamValue;
        pIsEnum = aIsEnum;
        pEnumList = aEnumList;
    }

    public StreamParameter(bool aIsFBlock, string aParamName, string aParamType, string aParamValue)
        : base(false, aIsFBlock, aParamName)
    {
        pParamType = aParamType;
        pParamValue = aParamValue;
        pIsEnum = false;
        pEnumList = null;
    }
}

StreamParameterContainer

 [DataContract]
[KnownType(typeof(StreamParameterContainer))]
public class StreamParameterContainer : BaseStreamParameter
{
    private List<BaseStreamParameter> pStreamParameters;
    public StreamParameterContainer(string aParamName)
        : base(aParamName)
    {
        pStreamParameters = new List<BaseStreamParameter>();
    }
    [DataMember]
    public List<BaseStreamParameter> StreamParameters
    {
        get { return pStreamParameters; }
        set { pStreamParameters = value; }
    }

    public int GetCountOfNonRepeatingParameters()
    {
        int count = 0;
        foreach (BaseStreamParameter bsp in pStreamParameters)
        {
            if (bsp is StreamParameter)
                count++;
        }
        return count;
    }
}

所以我有一个StreamParameterContainer对象,其StreamParameters属性包含一个RepeatableStreamParameter,其RepeatableParameters属性包含7个StreamParameter对象。当我调用WCF Web服务时,这一切都是正确的。另一方面,RepeatableStreamParameter的RepeatableParameters属性为空。

任何人都可以告诉我原因吗? 在我在客户端进行呼叫之后,但在它到达服务器之前,有什么东西可以看到发送的是什么?

2 个答案:

答案 0 :(得分:1)

我用以下代码测试了你的代码:

DataContractSerializer serializer = new DataContractSerializer(typeof(BaseStreamParameter));

MemoryStream stream = new MemoryStream();
var parameter = new BaseStreamParameter.RepeatableStreamParameter(true, "test");
parameter.RepeatableParameters.Add(new BaseStreamParameter("test2"));
serializer.WriteObject(stream, parameter);

stream.Position = 0;
var read = (BaseStreamParameter.RepeatableStreamParameter)serializer.ReadObject(stream);
Console.WriteLine(read.RepeatableParameters[0].ParamName);

这打印出“test2”,正如所料。

我承认这不是你问题的答案,但我很好奇你是否/为什么看不到相同的结果。

答案 1 :(得分:0)

服务器上的dll已过期。当我更新一切正常时。