LINQ和对象引用的麻烦

时间:2013-03-31 20:51:01

标签: c# linq collections

我试图在以下几行中尝试工作,这是一个完整的代码示例,可以在Visual Studio中运行,演示我正在谈论的场景。这里的所有内容都按预期工作,当我更改播放器的IsReady值时,会触发附加到事件的lambda以及if(to)计算结果为true。但是,Console.WriteLine永远不会被命中,因为看起来ConcurrentBag中播放器的IsReady值没有更新。

class Program
{
    public static ConcurrentBag<Player> Players { get; set; }
    static void Main(string[] args)
    {
        Players = new ConcurrentBag<Player>();
        Player player = new Player() { Id = "123" };
        Players.Add(player);
        player.IsReady.ValueChanged += (from, to) =>
        {
            if (to)
            {
                if (Players.All(p => p.IsReady.Value))
                {
                    Console.WriteLine("It worked");
                }
            }
        };

        LookupPlayerById("123").IsReady.Value = true;
    }

    public static Player LookupPlayerById(string clientId)
    {
        var player = Players.FirstOrDefault(x => x.Id == clientId);
        return player;
    }
}

public class Player
{
    public string Id { get; set; }
    public MonitoredValue<bool> IsReady { get; set; }

    public Player()
    {
        IsReady = new MonitoredValue<bool>(false);
    }
}

public class MonitoredValue<T>
{
    public delegate void ValueChangedHandler(T from, T to);
    public event ValueChangedHandler ValueChanged;

    private T m_Value;
    public T Value
    {
        get { return m_Value; }
        set
        {
            if (ValueChanged != null) // if invocation list is not empty, fire the event
            {
                ValueChanged(m_Value, value);
            }
            m_Value = value;
        }
    }

    public MonitoredValue() { }

    public MonitoredValue(T initialValue)
    {
        m_Value = initialValue;
    }
}

2 个答案:

答案 0 :(得分:3)

问题是您首先通知您的听众,然后实际更改该值。 将您的属性定义更改为以下代码:

delegate {}如果默认事件实现,现在我不需要在每次调用之前检查null)

public event ValueChangedHandler ValueChanged = delegate {};

public T Value
{
    get { return m_Value; }
    set
    {
        //first change
        m_Value = value;

        //now notify
        ValueChanged(m_Value, value);

    }
}

答案 1 :(得分:0)

由于它是一个引用类型,所以Player会被FirstOrDefault返回的对象引用,除非它是一个默认值,在这种情况下你没有得到匹配。

演示示例:

    public static void Main()
    {
        ConcurrentBag<ClassA> test = new ConcurrentBag<ClassA>();
        var hurp = new ClassA();
        hurp.number = 3;
        test.Add(hurp);

        var derp = test.FirstOrDefault();
        derp.number = 4;

        Console.Write(test.FirstOrDefault().number);
        Console.WriteLine(derp.number);
        Console.ReadLine();
    }

打印: 44