协变指派不起作用

Wat*_* v2 3 .net c# system.reactive

我试图概括通过一个对象的属性的更改通知,IObservable<T>但这是问题的重点.问题的关键在于:以下分配我报告了无效的协方差错误:

protected virtual void OnPropertyChanged<T>(
                      string propertyName, 
                      T oldValue, T newValue)
{
    IPropertyChangedNotification<Student, object> notification = 
        new PropertyChangedNotification<Student, T>(this,
        propertyName, oldValue, newValue);

    ...
}
Run Code Online (Sandbox Code Playgroud)

这报告:

无法隐式转换 PropertyChangedNotification<UsingSubjectToWatchProperty.Student, T>IPropertyChangedNotification<UsingSubjectToWatchProperty.Student, object>.存在显式转换(您是否错过了演员?)

这是完整的代码:

class Student
{
    private ISubject<IPropertyChangedNotification<Student, object>> _subject = 
       new Subject<IPropertyChangedNotification<Student, object>>();;
    private string _name = null;

    public string Name
    {
        get
        {
            return _name;
        }
        set
        {

            var oldValue = _name;
            _name = value;

            OnPropertyChanged<string>("Name", oldValue, _name);
        }
    }

    protected virtual void OnPropertyChanged<T>(string propertyName, T oldValue, T newValue)
    {
        IPropertyChangedNotification<Student, object> notification = 
            new PropertyChangedNotification<Student, T>(this,
            propertyName, oldValue, newValue);

        _subject.OnNext(notification);
    }
}

public class PropertyChangedNotification<TDeclaringType, TPropertyType>
    : IPropertyChangedNotification<TDeclaringType, TPropertyType>
{
    public PropertyChangedNotification(TDeclaringType declaringObject, 
        string propertyName, 
        TPropertyType oldValue, 
        TPropertyType newValue)
    {
        DeclaringObject = declaringObject;
        PropertyName = propertyName;
        OldValue = oldValue;
        NewValue = newValue;
    }

    public TDeclaringType DeclaringObject { get; set; }
    public string PropertyName { get; set; }

    public TPropertyType OldValue { get; protected set; }
    public TPropertyType NewValue { get; protected set; }
}

public interface IPropertyChangedNotification<TDeclaringType, out TPropertyType>
{
    TDeclaringType DeclaringObject { get; set; }
    string PropertyName { get; set; }

    TPropertyType OldValue { get; }
    TPropertyType NewValue { get; }
}
Run Code Online (Sandbox Code Playgroud)

PS:这不是生产代码.只是练习东西.

Eli*_*bel 5

只有参考类型支持协方差和逆变(将值类型转换为Object需要装箱的值).

所以你需要约束Tclass:

void OnPropertyChanged<T>(string propertyName, T oldValue, T newValue)
    where T : class { ... }
Run Code Online (Sandbox Code Playgroud)

或者,你可以使用new PropertyChangedNotification<Student, object>().

第三种选择是拥有一个没有的接口TProperty:

public interface IPropertyChangedNotification<TDeclaringType>
{
    TDeclaringType DeclaringObject { get; set; }
    string PropertyName { get; set; }
}

public interface IPropertyChangedNotification<TDeclaringType, out TPropertyType>
    :  IPropertyChangedNotification<TDeclaringType>
{
    TPropertyType OldValue { get; }
    TPropertyType NewValue { get; }
}
Run Code Online (Sandbox Code Playgroud)

然后在它中使用它Subject(因为在订阅时无论如何都必须将它转换为具体类型):

ISubject<IPropertyChangedNotification<Student>>
Run Code Online (Sandbox Code Playgroud)