可能重复:
在成员初始化程序中不能使用'this'?
如果我尝试做这样的事情,为什么我会收到错误的任何想法:
public class Bar
{
public Bar(Foo foo)
{
}
}
public class Foo
{
private Bar _bar = new Bar(this);
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误说:
"不能在成员初始化程序中使用'this'"
但以下工作:
public class Foo
{
private Bar _bar;
public Foo()
{
_bar = new Bar(this);
}
}
Run Code Online (Sandbox Code Playgroud)
有谁知道这背后的原因?我的理解是这些会编译成同一个IL,所以很好奇为什么一个被允许而另一个不被允许.
谢谢,亚历克斯
我想找出如何模拟一个方法,它在第二次调用它时第二次返回一个不同的值.例如,像这样:
public interface IApplicationLifetime
{
int SecondsSinceStarted {get;}
}
[Test]
public void Expected_mock_behaviour()
{
IApplicationLifetime mock = MockRepository.GenerateMock<IApplicationLifetime>();
mock.Expect(m=>m.SecondsSinceStarted).Return(1).Repeat.Once();
mock.Expect(m=>m.SecondsSinceStarted).Return(2).Repeat.Once();
Assert.AreEqual(1, mock.SecondsSinceStarted);
Assert.AreEqual(2, mock.SecondsSinceStarted);
}
Run Code Online (Sandbox Code Playgroud)
有什么能使这成为可能吗?除了为实现状态机的getter实现一个sub?
干杯fellas,
亚历克斯
我遇到的一个用例,我怀疑我不能成为唯一一个用例,如下所示:
IObservable<T> Observable.RepeatLastValueDuringSilence(this IObservable<T> inner, TimeSpan maxQuietPeriod);
Run Code Online (Sandbox Code Playgroud)
这将从内部observable返回所有未来的项目,但是,如果内部observable在一段时间内没有调用OnNext(maxQuietPeriod),它只重复最后一个值(当然内部调用OnCompleted或OnError) .
理由是服务定期ping出定期状态更新.例如:
var myStatus = Observable.FromEvent(
h=>this.StatusUpdate+=h,
h=>this.StatusUpdate-=h);
var messageBusStatusPinger = myStatus
.RepeatLastValueDuringSilence(TimeSpan.FromSeconds(1))
.Subscribe(update => _messageBus.Send(update));
Run Code Online (Sandbox Code Playgroud)
这样的事情存在吗?还是我过度估计它的用处?
谢谢,亚历克斯
PS:我为任何不正确的术语/语法道歉,因为我只是第一次探索Rx.
我的应用程序中有一个错误,只有当我在调试器中暂停应用程序几分钟时,它才能显示出来.我怀疑这是由于我使用的第三方网络库有一个心跳线程,当它的心跳线程暂停时它无法ping服务器时会断开连接.
我正在尝试为此编写一个测试用例应用程序,以验证这是导致该错误的原因.为此,我需要一种方法来暂停应用程序中的所有线程(我稍后将其缩小到仅暂停我怀疑可能是心跳线程的线程)来模拟在调试器中暂停应用程序.
有谁知道如何做到这一点?一个线程甚至可能导致另一个线程入睡吗?
谢谢,亚历克斯
更新:
我最终决定我真的不需要一个应用程序为我这样做,因为重点是验证调试器中的暂停导致断开连接.所以,这就是我所做的......(最简单的方法往往是最好的......或者至少是最简单的......)
private static void Main(string[] args)
{
IPubSubAdapter adapter = BuildAdapter();
bool waitingForMessage;
adapter.Subscribe(_topic, message => waitingForMessage = false, DestinationType.Topic);
Stopwatch timePaused = new Stopwatch();
while (adapter.IsConnected)
{
Console.WriteLine("Adapter is still connected");
waitingForMessage = true;
adapter.Publish(_topic, "testmessage", DestinationType.Topic);
while (waitingForMessage)
{
Thread.Sleep(100);
}
timePaused.Reset();
timePaused.Start();
Debugger.Break();
timePaused.Stop();
Console.WriteLine("Paused for " + timePaused.ElapsedMilliseconds + "ms.");
Thread.Sleep(5000); // Give it a chance to realise it's disconnected.
}
Console.WriteLine("Adapter is disconnected!");
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
并输出:
Adapter is still connected …Run Code Online (Sandbox Code Playgroud)