小编Wen*_*eng的帖子

C# 无法从“ref xxx”转换为“ref object”

我定义了一个使用 ref 对象作为参数的方法。当我尝试使用引用列表调用它时,它告诉我无法从引用列表转换为引用对象。我做了很多搜索来找到答案。然而,大多数答案是“你不需要在这里参考”或者有解决方法。

即使使用 ref (Base)[Inherited],似乎也无法从“ref [Inherited]”转换为“ref [Base]”。不知道我说得对不对。

我想要的是在 set { } 块中仅写入 1 行来更改值并发送通知。有什么建议么?

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, ...);};
    }
}
Run Code Online (Sandbox Code Playgroud)

c# ref inotifypropertychanged

6
推荐指数
1
解决办法
3843
查看次数

标签 统计

c# ×1

inotifypropertychanged ×1

ref ×1