0

我有以下课程

[DataContract(Name = "TemplateParameter")]
public class TemplateParameter : Mvvm.PropertyChangedBase
{
    #region FIELDS
    private string _key, _description, _defaultValue;
    private TemplateParameterValueType _valueType;
    #endregion

    [DataMember(Name = "Key")]
    public string Key 
    {
        get { return _key; }
        set
        {
            if (value != _key)
            {
                _key = value;
                OnPropertyChanged("Key");
            }
        }
    }

    [DataMember(Name = "Description")]
    public string Description
    {
        get { return _description; }
        set
        {
            if (value != _description)
            {
                _description = value;
                OnPropertyChanged("Description");
            }
        }
    }

    [DataMember(Name = "DefaultValue")]
    public string DefaultValue
    {
        get { return _defaultValue; }
        set
        {
            if (value != _defaultValue)
            {
                _defaultValue = value;
                OnPropertyChanged("DefaultValue");
            }
        }
    }

    [DataMember(Name = "ValueType")]
    public TemplateParameterValueType ValueType
    {
        get { return _valueType; }
        set
        {
            if (value != _valueType)
            {
                _valueType = value;
                OnPropertyChanged("ValueType");
            }
        }
    }

    public TemplateParameter()
    {
        Key = "Parametername";
        Description = "Parameterbeschreibung";
        DefaultValue = "";
        ValueType = TemplateParameterValueType.String;
    }
}

最近添加了OnPropertyChanged代码,我将继承更改为从PropertyChangedBase派生。

[DataContract]
public class PropertyChangedBase : INotifyPropertyChanged
{
    #region INotifyPropertyChanged
    /// <summary>
    /// Raised when a property on this object has a new value.
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Raises this object's PropertyChanged event.
    /// </summary>
    /// <param name="propertyName">The property that has a new value.</param>
    protected virtual void OnPropertyChanged(string propertyName)
    {
        this.VerifyPropertyName(propertyName);

        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }
    #endregion

    #region Debugging aids
    /// <summary>
    /// Warns the developer if this object does not have
    /// a public property with the specified name. This 
    /// method does not exist in a Release build.
    /// </summary>
    [Conditional("DEBUG")]
    [DebuggerStepThrough]
    public void VerifyPropertyName(string propertyName)
    {
        // Verify that the property name matches a real,  
        // public, instance property on this object.
        if (TypeDescriptor.GetProperties(this)[propertyName] == null)
        {
            string msg = "Invalid property name: " + propertyName;

            if (this.ThrowOnInvalidPropertyName)
                throw new Exception(msg);
            else
                Debug.Fail(msg);
        }
    }

    /// <summary>
    /// Returns whether an exception is thrown, or if a Debug.Fail() is used
    /// when an invalid property name is passed to the VerifyPropertyName method.
    /// The default value is false, but subclasses used by unit tests might 
    /// override this property's getter to return true.
    /// </summary>
    protected virtual bool ThrowOnInvalidPropertyName { get; private set; }

    #endregion
}

在更改继承之前,该类已作为其他类的组件序列化为文件。更改继承后,我在反序列化时收到以下错误消息:

意外的错误。xyz 行出错。元素 'DefaultValue' ... 出乎意料。需要元素“PropertyChanged”。

我删除了继承并从PropertyChangedBase复制了代码,反序列化成功。

我试图了解这里发生了什么。

  • 为什么继承会破坏序列化而内联代码不会?
  • 为什么继承时需要 PropertyChanged 而内联时不需要?
  • 错误消息的真正含义是什么?

bei System.Runtime.Serialization.XmlObjectSerializerReadContext.ThrowRequiredMemberMissingException(XmlReaderDelegator xmlReader, Int32 memberIndex, Int32 requiredIndex, XmlDictionaryString[] memberNames)
bei System.Runtime.Serialization.XmlObjectSerializerReadContext.GetMemberIndexWithRequiredMembers(XmlReaderDelegator xmlReader, XmlDictionaryString[] memberNames, XmlDictionaryString[] memberNamespaces, Int32 memberIndex, Int32 requiredIndex, ExtensionDataObject extensionData)
bei ReadTemplateParameterFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] )
bei System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
bei System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
bei System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)
bei System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, String name, String ns)
bei ReadArrayOfTemplateParameterFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString , XmlDictionaryString , CollectionDataContract )
bei System.Runtime.Serialization.CollectionDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
bei System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
bei System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)
bei System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, String name, String ns)
bei ReadUnitOfTestFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] )
bei System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
bei System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
bei System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)
bei System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, String name, String ns)
bei ReadArrayOfBaseModuleFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString , XmlDictionaryString , CollectionDataContract )
bei System.Runtime.Serialization.CollectionDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
bei System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
bei System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)
bei System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserialize(XmlReaderDelegator xmlReader, Type declaredType, DataContract dataContract, String name, String ns)
bei System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
bei System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
bei System.Runtime.Serialization.DataContractSerializer.ReadObject(XmlReader reader)
bei Pruef.Net.Model.ModuleManager.LoadModules() in D:\\my_workspaces\\visualstudioprojects\\Pruef.Net\\Pruef.Net\\PruefMvvm\\Model\\ModuleManager.cs:Zeile 630."
4

0 回答 0