The quirky ?:

If there was ever a poll conducted for the most favorite C# operator, I’d guess the conditional operator ?:, also known as the ternary operator, will win hands down. I find it to be one of those tools that make things shorter and clearer at the same time. But how well do you know the operator? What do you think is wrong with the following piece of code?     interface IDoer {}    class ADoer : IDoer { }    class BDoer : IDoer { }    class Program    {        static void Main(string[] args)        {            bool flag = bool.Parse(args[0]);            IDoer doer = flag … Continue reading The quirky ?:

Playing with IronPython

I’ve been dabbling with IronPython for the last few days, trying to host it and use it as a scripting engine. I have to say that I’m impressed. The hosting API is incredibly simple to use and Mike Stall’s blog served as a handy reference. One thing I wanted my scripts to do was to access private and internal members of types in the hosting application. I found that the command line python interpreter (ipy.exe) had a -X:PrivateBinding switch to allow access to private and internal members using the <type>_<type>__memberName convention (which seems to be the standard python name mangling … Continue reading Playing with IronPython

Add logs to the start and end of methods

 MethodLogger is a macro I wrote that adds log statements at the beginning and ending of all methods in the current document in Visual Studio. Unzip the file, load it in Visual Studio using Tools->Macros->Load Macro Project, open the appropriate C# file and then run the LogMethodStartAndEnd macro. It transforms class SomeClass{ public void SomeMethod() { MessageBox.Show(“Doodly doodly Doo”); }} to class SomeClass{ public void SomeMethod() { Console.WriteLine(“Start of SomeClass.SomeMethod()”); try { MessageBox.Show(“Doodly doodly Doo”); } finally { Console.WriteLine(“End of SomeClass.SomeMethod()”); } }} The code, written in VB .NET (which seems to be the only supported language for writing macros?) … Continue reading Add logs to the start and end of methods

New version of deadlock detector

A new version of deadlock detector is up at Deadlock Detector^. This version is a partial rewrite, with a modified version of the algorithm to detect the object being locked. It now does a virtual execution of the IL, following through all control paths to find the locked object. There are a few issues with the execution logic, sometimes resulting in stack overflows and infinite loops. Any comments/suggestions/bug reports welcome.

How could this work?

I’m sure most of you know about BackgroundWorker, a new component in .NET 2.0, that allows UI code to run tasks asynchronously, without running into cross thread update issues. But what would happen if the thread that launches the background worker is not a UI thread i.e. it doesn’t run a message pump? The answer is quite surprising and caused a piece of working code like this AutoResetEvent waitForWorker = new AutoResetEvent(false);void Method(){ BackgroundWorker worker = new BackgroundWorker(); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_Completed) worker.RunAsync(); waitForWorker.WaitOne();}void worker_Completed(…){ waitForWorker.Set();} to stop working after making a tiny modification like this class MyForm : Form … Continue reading How could this work?