Create a Class in .NET without calling it’s Constructor (and other .NET dark magics)

I stumbled on something kind of useful the other day that I figured I’d share. I was messing around with .NET’s cool Reflection.Emit functionality for dynamically creating classes at runtime but I kept running into a problem: despite my best efforts to add a constructor to my classes dynamically, GetType().GetConstructor() always returned null. The only way I know of to create an object at runtime is to Invoke() the constructor, so the null constructor was obviously problematic. The classes I was creating were really more structlike, there wasn’t any particularly important functionality that I needed to put into the constructor… I just needed a way to new up the object.

That’s when a friend at work pointed me at the FormatterServices class in the System.Runtime.Serialization namespace. It turns out you can call FormatterServices.GetUninitializedObject() and .NET will create the object in memory without invoking the constructor. I have no idea whether or not this is a recommended approach, it definitely seems like a bit of black magic. Doubtless I could have gotten around the issue in the first place by dabbling a bit more in the realm of ConstructorBuilders and IL, but this approach definitely works for what I need and maybe it will save someone else some grief as well.

Why I Bit the Bullet (& IE’s Box Model CSS Bug)

>As a Computer Programmer (by trade and for fun) I’m obviously ever reliant on the ability to search the web for info… whether it’s parsing documentation, combing bulletin boards to uncover the solution to the daily problems that someone else has doubtless encountered, or the occasional hail mary when you have no clue what’s going on.

Lately I’ve been burning a bid of the midnight oil on a pet project, and tonight while trying to dust the cobwebs off of my HTML/CSS skills I realized that IE and Firefox were rendering my page differently. I remembered having bumped into the problem before but being rusty I had to go fishing and I found the solution on someone else’s blog. The issue was that IE isn’t standards compliant in terms of the way it behaves when rendering if padding and/or margins are specified. Firefox correctly adds the padding/margins to the size of the div (or whatever you’re applying the style to) while IE doesn’t. There is hope however, rather than going to wild extremes to hack the code to behave differently for different browsers you can just include something like the following at the top of your HTML document to include the proper document type definition:

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>

or

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>

The bug is called the “Box Model Bug for IE”, if you’re really curious you can find more info here. Not the most interesting problem in the world, but it made me realize that rather than just being a leecher in the fount of Internet crap (and wisdom) I too can be a seeder, and thus I begin my own blog about my ventures in coding and all things related to Software Engineering.