我试图通过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>
至CCD_ 3。存在显式转换(是否缺少强制转换?)
这是完整的代码:
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>>