C# Collection Initializers via Extension Methods

An interesting C# feature that I just stumbled across by accident: You can provide a custom collection initializer for any IEnumerable<T> using an extension method. It allows you to turn something like this:

into this:

by simply providing the Add method that is required for collection initializers as an extension method:

Definitely not something for everyday use, but I can imagine it could clean up some data-heavy bits of code a lot. It appears that this was introduced in C# 6.0, along with indexer initializers.

Read more …

Conditional Details

If you’ve programmed in C# for a while, you’ve probably stumbled upon the Conditional attribute. It allows you to specify that a certain method will only be called when a certain compilation symbol is defined. Let’s check the details of that.

Read more …

Ownership Inversion in Delegates

As you probably know, I’m currently busy rewriting the Cloning system in Duality. While you can read most of my experiences doing so in my last blog entry, there is a peculiar little problem case that revealed itself just recently. It should be solved by now, but I thought it might be interesting enough for its own followup entry. Let’s call it Ownership Inversion in Delegates.

Read more …

How to Clone an Object

Making an exact copy of an object isn’t an everyday use case for most programming APIs, but there are certain tasks where the ability to do so is vital. It’s not an easy task per se, and it requires a lot of thought when executed on a larger scale. When designing a modular framework like Duality, where every user can easily add custom classes into the realm of conveniently automated behavior, things get even worse. Fingers crossed, I might finally have found a solution.

Read more …

A custom PropertyGrid

I’m working on some kind of custom PropertyGrid Control right now. You might have spottet it in some of the Screenshots that I’ve previously posted. .Net does have an existing PropertyGrid which is customizable to some degree and I could just have used it – but after doing that in Nullpunkt and not being very happy with the results I’m now trying a different approach.

Read more …

Serialization pitfall: Post-Deserialization

If you use .Net Serialization, especially Deserialization, you might want to have an object perform a specific action as soon as it has been deserialized, initialize some cache data, resolve references or something.

Read more …

C# 4.0 powers, ACTIVATE!

When I began to write ZweiDe back in 2007, the target framework was .Net 2.0. The third framework version was already released, but I assumed it would severely impact compatibility to require the newest stuff available. As it took years to bring ZweiDe anywhere near release, it was of course irrelevant. I didn’t care much since I had no practical idea of what I was missing anyway. In Duality, calculating with an estimated development time of forever, I figured out it might be worth a try targeting .Net 4.0 right from start.

Read more …

Picking Colors

Have you ever used the .Net Color Picker Dialog? I did for a while but wasn’t quite content with it. For the Duality Editor, I don’t need any dialog-internal predefined colors and the additional clicks to define and choose a custom color reduce its usability. Also, there is no way to select a color with alpha channel support. After evaluating some other .Net ColorPicker modules out there, I decided to write my own. You can see the result on the right. If you need something similar, feel free to download its source code (Visual Studio Solution) and use it in your own project.

Read more …

Logging

There’s really nothing much to say about logging. I just thought, I’d share some code that might spare you some time writing it yourself. Basically a Log class. Create an instance for each separate Log type (e.g. Game, Core, Editor, …) and assign n ILogOutput instances to each of them. There are ILogOutput classes for System.Console or a TextWriter in general (any Stream).

Read more …