本文整理汇总了C#中Publisher.Raise方法的典型用法代码示例。如果您正苦于以下问题:C# Publisher.Raise方法的具体用法?C# Publisher.Raise怎么用?C# Publisher.Raise使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Publisher
的用法示例。
在下文中一共展示了Publisher.Raise方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Handlers_Can_Be_Unsubscribed
public void Handlers_Can_Be_Unsubscribed()
{
var pub = new Publisher();
var calledSubscribers = new List<int>();
var sub1 = new InstanceSubscriber(1, pub, calledSubscribers.Add);
var sub2 = new InstanceSubscriber(2, pub, calledSubscribers.Add);
StaticSubscriber.FooWasRaised = false;
StaticSubscriber.Subscribe(pub);
// Make sure they really were subscribed
pub.Raise();
calledSubscribers.Should().Equal(1, 2);
StaticSubscriber.FooWasRaised.Should().BeTrue();
calledSubscribers.Clear();
sub1.Unsubscribe(pub);
pub.Raise();
calledSubscribers.Should().Equal(2);
StaticSubscriber.FooWasRaised = false;
StaticSubscriber.Unsubscribe(pub);
pub.Raise();
StaticSubscriber.FooWasRaised.Should().BeFalse();
calledSubscribers.Clear();
sub2.Unsubscribe(pub);
pub.Raise();
calledSubscribers.Should().BeEmpty();
// Make sure subscribers are not collected before the end of the test
GC.KeepAlive(sub1);
GC.KeepAlive(sub2);
}
示例2: Multicast_Handlers_Are_Called
public void Multicast_Handlers_Are_Called()
{
var pub = new Publisher();
var calledSubscribers = new List<int>();
EventHandler<EventArgs> handler = null;
handler += (sender, e) => calledSubscribers.Add(1);
handler += (sender, e) => calledSubscribers.Add(2);
handler += (sender, e) => calledSubscribers.Add(3);
pub.Foo += handler;
pub.Raise();
calledSubscribers.Should().Equal(1, 2, 3);
}
示例3: Instance_Handlers_Are_Called
public void Instance_Handlers_Are_Called()
{
var pub = new Publisher();
var calledSubscribers = new List<int>();
var sub1 = new InstanceSubscriber(1, pub, calledSubscribers.Add);
var sub2 = new InstanceSubscriber(2, pub, calledSubscribers.Add);
pub.Raise();
calledSubscribers.Should().Equal(1, 2);
// Make sure subscribers are not collected before the end of the test
GC.KeepAlive(sub1);
GC.KeepAlive(sub2);
}
示例4: handlerNotCalledDueToException
public void handlerNotCalledDueToException()
{
bool thirdHandlerCalled = false;
bool exceptionHandled = false;
Publisher pub = new Publisher();
pub.OnChange += (sender, args) => { Console.WriteLine("All is good");};
pub.OnChange += (sender, args) => { throw new Exception("All is bad."); };
pub.OnChange += (sender, args) => {
thirdHandlerCalled = true;
Console.WriteLine("Do I even get called?");
};
try {
pub.Raise();
}catch(Exception)
{
exceptionHandled = true;
}
Assert.IsTrue(exceptionHandled);
Assert.IsFalse(thirdHandlerCalled);
}
示例5: Subscribers_Can_Be_Garbage_Collected
public void Subscribers_Can_Be_Garbage_Collected()
{
var pub = new Publisher();
var calledSubscribers = new List<int>();
var sub1 = new InstanceSubscriber(1, pub, calledSubscribers.Add);
var sub2 = new InstanceSubscriber(2, pub, calledSubscribers.Add);
var weakSub1 = new WeakReference(sub1);
var weakSub2 = new WeakReference(sub2);
// ReSharper disable once RedundantAssignment
sub2 = null; // only necessary in Debug
GC.Collect();
GC.WaitForPendingFinalizers();
weakSub1.IsAlive.Should().BeTrue("because it is explicitly kept alive (sanity check)");
weakSub2.IsAlive.Should().BeFalse("because it should have been collected");
pub.Raise();
calledSubscribers.Should().Equal(1);
// Make sure sub1 is not collected before the end of the test (sub2 can be collected)
GC.KeepAlive(sub1);
}
示例6: Static_Handlers_Are_Called
public void Static_Handlers_Are_Called()
{
var pub = new Publisher();
StaticSubscriber.FooWasRaised = false;
StaticSubscriber.Subscribe(pub);
pub.Raise();
StaticSubscriber.FooWasRaised.Should().BeTrue();
}