协变分配不起作用

水冷却器v2

我试图通过一个对象来概括对对象属性的更改的通知,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);

    ...
}

该报告:

无法将类型隐式转换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; }
}

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

伊丽·阿贝尔

仅参考类型支持协方差和协方差(要将值类型转换为Object,需要装箱)。

因此,您需要限制Tclass

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章