I’ve been meaning to post this for a couple of months now. Recently, in my free time, I wrote a wrapper for WinPcap for .NET. I did it partially to get a better understanding of native interop, and partially because the existing WinPcap wrapper for .NET had some fundamental design issues that made it a pain to use.
I learned a lot about writing safe and efficient P/Invoke code in the process, as well as had some things click for me in API design. Both of which I will write posts on later. The gist is that I tried to design the library to remain faithful to C# and not the underlying C API. The following is a simple program that prints out the lengths of each packet it sniffs from the network:
using System; using System.Linq; using Spladug.PacketCapture; static class Program { static void Main() { var device = PacketCaptureDevice .GetAllPacketCaptureDevices() .First(); using (var reader = device.OpenRead()) { foreach (var packet in reader.ReadPackets()) { Console.WriteLine(packet.Data.Length); } } } }
The public API is fully documented with XML doc comments, and HTML documentation is on its way. In the mean time, head over to the project homepage and check it out.
I spent a couple of days last week writing a (very) simple Sudoku solver in WPF. For the most part, everything I wanted to do was ridiculously easy to achieve with WPF. However, there was one point that took me an hour or two of research to figure out: switching DataTemplates dynamically.
To draw my Sudoku board, I data bind to some model-objects that describe the board. Each cell on the board has a value (a nullable int), and a list of values that could possibly go in that cell. The effect I wanted to achieve was to show the value for a cell if it’s not null, and show a grid of possible values if not yet determined:

I finally found the answer in a comment by Andy Kutruff on a post asking a very similar question to mine . There’s a minor skirmish in the comments about whether or not Andy’s solution is injecting business logic into the presentation layer, but I feel that his solution is the correct one for this application because the two templates are purely a UI concern, and not related to the functioning of the model.
The basic idea is to use a DataTemplate containing a single ContentPresenter as a selector. The ContentPresenter has a ContentTemplate set on it that becomes the default template. Data triggers on the template are then used to switch between the various sub-templates for the ContentPresenter. The following are reformatted excerpts from the XAML code for my sudoku solver. To see the full source code for the project, look at the code page. Please excuse the lack of syntax highlighting for XAML; I need to figure out something for that.
To start, we need to make a DataTemplate that is used by the control in question. This template simply contains a ContentControl whose contents will be swapped out based on data.
<DataTemplate x:Key="CellTemplate"> <ContentControl ContentTemplate="{StaticResource CellSelectorTemplate}" Content="{Binding}" /> </DataTemplate>
Finally, we create the template that does the selecting of sub-templates. This works by using data triggers to change the ContentPresenter’s ContentTemplate.
<DataTemplate x:Key="CellSelectorTemplate"> <ContentPresenter x:Name="Presenter" ContentTemplate="{StaticResource CellWithValueTemplate}" Content="{TemplateBinding Content}" /> <DataTemplate.Triggers> <DataTrigger Binding="{Binding Value}" Value="{x:Null}"> <Setter TargetName="Presenter" Property="ContentTemplate" Value="{StaticResource CellWithoutValueTemplate}" /> </DataTrigger> </DataTemplate.Triggers> </DataTemplate>
After that, it’s just creating the templates for each of the states needed. The data trigger switches between the templates depending on the data, and the templates do their magic.
In my continued work on the XNA project, I recently worked on an input system that supported key bindings. What I wanted was to be able to bind a key / button to a handler method in such a way that I could call the binding on whichever object currently had focus for input. C++’s pointer-to-member-function syntax instantly sprang to mind, so I poked around in C++/CLI’s documentation to see how they implemented it in .NET.
The answer is open-instance delegates (or unbound delegates as they’re called in C++/CLI). Though C# has no support in its syntax for open-instance delegates, you can still create and use them.
The trick is to create a delegate type that takes the same parameters as the method you want to bind to, but includes an extra parameter for the instance at the beginning of the parameter list. Given this class with a single method:
class InputSink { public void OnExitButtonPressed(object sender, EventArgs args) { // ... } }
The delegate and code to create it would look like this:
delegate void Handler(InputSink @this, object sender, EventArgs args); var method = typeof(InputSink).GetMethod("OnExitButtonPressed"); return (Handler)Delegate.CreateDelegate(typeof(Handler), null, method);
The delegate is now an open-instance delegate that can be used on any object of or derived from InputSink. From there, it’s easy to throw the new delegates into a dictionary using the XNA Buttons (such as Buttons.Start, Buttons.A, etc.) as a key and easily look up bindings at runtime. To use the delegate you just pass an instance as the first parameter like so:
var inputSink = new InputSink(); Handler someHandler = GetHandler(); // assume this is the code block above someHandler(inputSink, sender, EventArgs.Empty);