In my new ongoing quest to read source code to be a better developer, I now present the sixth in an infinite number of a weekly series called "The Weekly Source Code." Here's some source I'm reading this week that I enjoyed.
public class Example { static int val; [RepeatTest(5, Timeout=500)] public void RepeatingTestMethod() { Thread.Sleep(100); Assert.Equal(2, 2); if (val == 0) { val++; Thread.Sleep(1000); } } } public class RepeatTestAttribute : TestAttribute { readonly int repeatCount; public RepeatTestAttribute(int repeatCount) { this.repeatCount = repeatCount; } public override IEnumerable<ITestCommand> CreateTestCommands(MethodInfo testMethod) { for (int index = 0; index < repeatCount; index++) yield return new TestCommand(testMethod); } }
public Record(string site, string salt, string encryptedUserId, string encryptedPassword, string encryptedNotes, string useSetWindowText, string duration, string nagSpan, string nextReminder, string lastReset, string usageCount) : this(site, salt, encryptedUserId, encryptedPassword, encryptedNotes, "true" == useSetWindowText, "" == duration ? 0 : Convert.ToInt32(duration), "" == nagSpan ? 0 : Convert.ToInt32(nagSpan), "" == nextReminder ? DateTime.MaxValue : Convert.ToDateTime(nextReminder), "" == lastReset ? DateTime.Now : Convert.ToDateTime(lastReset), "" == usageCount ? 0 : Convert.ToInt32(usageCount)) { }
namespace Rhino.Commons { public class DisposableAction<T> : IDisposable { Proc<T> _action; T _val; public DisposableAction(Proc<T> action, T val) { if (action == null) throw new ArgumentNullException("action"); _action = action; _val = val; } public T Value { get { return _val; } } public void Dispose() { _action(_val); } } public class DisposableAction : IDisposable { Proc _action; public DisposableAction(Proc action) { if (action == null) throw new ArgumentNullException("action"); _action = action; } public void Dispose(){ _action(); } } }
void CodeChanged(object sender, FileSystemEventArgs e) { string fileName = Path.GetFileNameWithoutExtension(e.FullPath); string typeName = controllersNamespace+"."+fileName; CompilerParameters options = CreateCompilerOptions(); CSharpCodeProvider provider = new CSharpCodeProvider(); CompilerResults compilerResults = provider .CompileAssemblyFromFile(options, e.FullPath); container.Kernel.RemoveComponent(typeName); if(compilerResults.Errors.HasErrors) return; Type type = compilerResults.CompiledAssembly.GetType(typeName); container.AddComponent(type.FullName, type); }
Feel free to send me links to cool source that you find hasn't been given a good read.
Did YOU know how easy it is to compile new code from within .NET?
Ads by The Lounge