C# 如何将发件人作为事件参数传输

C# 如何将发件人作为事件参数传输,c#,events,C#,Events,类的实例X注册到同一类的实例Y的更改事件 如果Y被更改,我想更新X,但我不想在整个类中使用static关键字。是否有方法在eventargs中传输事件的收件人 下面是一些带有NUnit测试的示例代码,以说明问题的确切位置。我编译并运行了它。两个测试验证了编程。考试不及格说明了我的问题 using System; using System.Collections.Generic; using System.Linq; using System.Text; using NUnit.Framework

类的实例X注册到同一类的实例Y的更改事件

如果Y被更改,我想更新X,但我不想在整个类中使用static关键字。是否有方法在eventargs中传输事件的收件人

下面是一些带有NUnit测试的示例代码,以说明问题的确切位置。我编译并运行了它。两个测试验证了编程。考试不及格说明了我的问题

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace Eventtest
{
    public class DependencyChain
    {
        public static int demonstrationcount = 0;

        private String hiddenstring = "";
        public String visiblestring
        {
            get { return hiddenstring; }
            set
            {
                hiddenstring = value;
                NotifyOnStringChanged();
            }
        }

        private void NotifyOnStringChanged()
        {
            if (changed != null)
            {
                changed(this, EventArgs.Empty);
            }
        } 

        public EventHandler changed;

        private EventHandler Observer = new EventHandler((o, e) => {
            DependencyChain sender = (o as DependencyChain);
            demonstrationcount++;
            //THE FOLLOWING DOES NOT WORK SINCE "this" IS NOT STATIC
            //DependencyChain recipient = this;
            //recipient.visiblestring = sender.visiblestring;
        });

        public DependencyChain(string initialstring)
        {
            this.visiblestring = initialstring;
        }

        public DependencyChain(DependencyChain other)
        {
            this.visiblestring = other.visiblestring;
            other.changed += Observer;
        }

        public override string ToString()
        {
            return visiblestring;
        }

    }

    [TestFixture]
    class Eventtest
    {
        [SetUp]
        public void ResetStaticCounter()
        {
            DependencyChain.demonstrationcount = 0;
        }

        [Test]//PASS
        public void ShouldInitialiseAndCopyValues()
        {
            DependencyChain Y = new DependencyChain("initial");
            DependencyChain X = new DependencyChain(Y);
            Assert.AreEqual(X.ToString(), Y.ToString());
        }

        [Test]//PASS
        public void ShouldCallObserverOnChange()
        {
            DependencyChain Y = new DependencyChain("initial");
            DependencyChain X = new DependencyChain(Y);
            Assert.AreEqual(0, DependencyChain.demonstrationcount);
            Y.visiblestring = "changed";
            Assert.AreEqual(1, DependencyChain.demonstrationcount);
        }

        [Test]//FAIL
        public void ShouldChangeStringOnChange()
        {
            DependencyChain Y = new DependencyChain("initial");
            DependencyChain X = new DependencyChain(Y);
            Y.visiblestring = "changed";
            Assert.AreEqual(X.ToString(), Y.ToString());
        }
    }
}

我认为您只需将
Observer
的初始化移动到
DependencyChain
的构造函数中,就可以捕获
这个

成功了。取消对代码的注释,移动到构造函数中名为method的方法。谢谢,很好。您还可以将evt处理程序编写为“普通”方法,而不是lambda方法。