这个字段怎么样?

Bob*_*orn 9 c#

我得到一个null异常,但该字段被初始化为一个空列表.那怎么可能是null?

此方法中的第二行发生错误(在_hydratedProperties上):

protected virtual void NotifyPropertyChanged<T>(Expression<Func<T>> expression)
{
    string propertyName = GetPropertyName(expression);

    if (!this._hydratedProperties.Contains(propertyName)) { this._hydratedProperties.Add(propertyName); }
}
Run Code Online (Sandbox Code Playgroud)

这就是宣布该领域的方式:

public abstract class EntityBase<TSubclass> : INotifyPropertyChanged where TSubclass : class
{
    private List<string> _hydratedProperties = new List<string>();
Run Code Online (Sandbox Code Playgroud)

这是如何设置:

    public Eta Eta
    {
        get { return this._eta; }

        set
        {
            this._eta = value;
            NotifyPropertyChanged(() => this.Eta);
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是完整的类(删除了注释和非相关部分):

[DataContract]
public abstract class EntityBase<TSubclass> : INotifyPropertyChanged where TSubclass : class
{
    private List<string> _hydratedProperties = new List<string>();

    public bool IsPropertyHydrated(string propertyName)
    {
        return this._hydratedProperties.Contains(propertyName);
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void NotifyPropertyChanged<T>(Expression<Func<T>> expression)
    {
        string propertyName = GetPropertyName(expression);

        if (!this._hydratedProperties.Contains(propertyName)) { this._hydratedProperties.Add(propertyName); }

        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public string GetPropertyName<T>(Expression<Func<T>> expression)
    {
        MemberExpression memberExpression = (MemberExpression)expression.Body;
        return memberExpression.Member.Name;
    }
}
Run Code Online (Sandbox Code Playgroud)

派生类:

[DataContract]
public class Bin : EntityBase<Bin>
{
    private Eta _eta;

    [DataMember]
    public Eta Eta
    {
        get { return this._eta; }

        set
        {
            this._eta = value;
            NotifyPropertyChanged(() => this.Eta);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Luc*_*ski 12

这是线索:

[DataContract]
Run Code Online (Sandbox Code Playgroud)

对.DataContractSerializer没有调用任何构造函数.相反,它用于FormatterServices.GetUninitializedObject创建将反序列化的对象.这会绕过构造函数调用.

你的初始化者:

private List<string> _hydratedProperties = new List<string>();
Run Code Online (Sandbox Code Playgroud)

由编译器转换为隐式默认构造函数.

作为解决方法,您可以使用反序列化回调OnDeserializingAttribute:

[DataContract]
public abstract class EntityBase<TSubclass> : INotifyPropertyChanged
    where TSubclass : class
{
    private List<string> _hydratedProperties;

    protected EntityBase()
    {
        Init();
    }

    private void Init()
    {
        _hydratedProperties = new List<string>()
    }

    [OnDeserializing]
    private void OnDeserializing(StreamingContext context)
    {
        Init();
    }

    // ... rest of code here
}
Run Code Online (Sandbox Code Playgroud)