协变分配不起作用

Covariant assignment not working

我试图通过 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);

    ...
}

此报告:

Cannot implicitly convert type PropertyChangedNotification<UsingSubjectToWatchProperty.Student, T> to IPropertyChangedNotification<UsingSubjectToWatchProperty.Student, object>. An explicit conversion exists (are you missing a cast?)

完整代码如下:

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; }
}

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

仅引用类型支持协变和逆变(要将值类型转换为 Object 需要装箱)。

因此您需要将 T 限制为 class:

void OnPropertyChanged<T>(string propertyName, T oldValue, T newValue)
    where T : class { ... }

或者,您可以只使用 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; }
}

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

ISubject<IPropertyChangedNotification<Student>>