我使用ref对象作为参数定义了一个方法。当我尝试用ref列表调用它时,它告诉我不能从ref列表转换为ref对象。为了找到答案,我做了很多搜索。然而,大多数的答案都是“您不需要这里的参考”或有工作周围。
即使使用继承的ref (Base),似乎也无法从“ref继承”转换为“ref Base”。不知道我说得对不对。
我想要的是只在set {}块中写入一行,以更改值并发送通知。有什么建议吗?
class CommonFunctions
{
public static void SetPropertyWithNotification(ref object OriginalValue, object NewValue, ...)
{
if(OriginalValue!= NewValue)
{
OriginalValue = NewValue;
//Do stuff to notify property changed
}
}
}
public class MyClass : INotifyPropertyChanged
{
private List<string> _strList = new List<string>();
public List<string> StrList
{
get { return _strList; }
set { CommonFunctions.SetPropertyWithNotification(ref _strList, value, ...);};
}
}发布于 2015-08-08 03:58:56
使用泛型和等号方法
class CommonFunctions
{
public static void SetPropertyWithNotification<T>(ref T OriginalValue, T NewValue)
{
if (!OriginalValue.Equals(NewValue))
{
OriginalValue = NewValue;
//Do stuff to notify property changed
}
}
}
public class MyClass
{
private List<string> _strList = new List<string>();
public List<string> StrList
{
get { return _strList; }
set { CommonFunctions.SetPropertyWithNotification(ref _strList, value); }
}
}https://stackoverflow.com/questions/31889481
复制相似问题