c# - Can I change current DateTime WITHOUT effecting other programs? -
I need to emulate the app that references dateTime.utcNow.
I want to change the time which has been refunded, but do not want to mess up your PC and work cycle etc.
Is it possible to return from the time of the date? Now more datamata ITC now, only in the context of the program? / C>
The easiest way to do this without implementing dependency injection everywhere and then in your simulation code, you can Do not forget you may also need to do this for DateTime UtcNow
With
SystemTime.UtcNow () and add the following class.
Public stable class SystemTime {public stable Func & lt; DateTime & gt; UtcNow = () = & gt; DateTime.UtcNow; }
SystemTime.UtcNow = () => With the back can change the code. New date time (2000,1,1);
DateTime.Now ,
DateTime.Today , etc. In addition, you can add a function so that you
SystemTime.Reset () .
Public stable class systemTime {public stable Func & lt; DateTime & gt; Can return to 'normal' behavior. UtcNow = () = & gt; DateTime.UtcNow; Public Static Func & lt; DateTime & gt; Now = () = & gt; DateTime.Now; Public Static Func & lt; DateTime & gt; Today = () = & gt; DateTime.Today; Public static zero reset () {UtcNow = () = & gt; DateTime.UtcNow; Now = () = & gt; DateTime.Now; Today = () = & gt; DateTime.Today; }}
Comments
Post a Comment