Deep Clone for .NET Objects

.NET objects don’t have a deep-copy function available, out of the box.  There is the ICloneable interface, of course, but there are still ongoing arguments over whether or not it actually implies “Deep Copy” functionality vs. “Shallow Copy.”

But for plain old CLR objects (POCOs), creating a deep clone is remarkably easy.  Just ensure that your class is marked as [Serializable] and implement this helpful utility class:

As you can see, if your class is Serializable, this utility class can just perform a quick serialize/deserialize to create a guaranteed deep clone of your POCO.

Thanks to StackOverflow, as always, for thinking of this long before I ever could.

Re-Enable “Shift-Enter” After Installing Visual Studio’s “Productivity Power Tools”

Before installing the Visual Studio “Productivity Power Tools” package from the Visual Studio Extension Manager, I had grown accustomed to some specific behavior in the Text Editor.

When you press Enter, you obviously trigger a “line break” (just like any other Text Editor in the world); but before installing the Power Tools, I could also trigger a line break when pressing Shift+Enter.  The Productivity Power Tools unfortunately re-assigned this keyboard mapping to some other trivial functionality (which I never use).

This seems like a trivial thing, but I actually took advantage of the “Enter”/”Shift-Enter” flexibility more often than I realized.  If I have an “if” statement, for example, I would press and hold the Shift key when I closed the parenthesis, and continue to hold the Shift key while I pressed Enter for the next line and opened up my code-block with a curly-brace ‘{’ and another New-Line.

In order to get it back, you have to go into the “Tools” Menu –> “Options…”

Continue reading

Extending MVVM Light With Attribute-Based Property Dependencies

MVVM Light is pretty great, but it’s not perfect.  In the past, I’ve rolled my own MVVM Framework, and sometimes I still struggle choosing between MVVM Light and my own stuff – if for no other reason than “it’s my own stuff.”

But one thing that always bothers me about MVVM Light is its distinct lack of any sort of Dependency mapping for properties. Any time you have a property whose value depends on another property (let’s say PropA depends on PropB), then you always need to manually raise the “PropertyChanged” event from PropB’s “Set” method.

In my opinion, this is a “bad thing.”  It’s not PropB’s responsibility to keep track of which properties depend on it.

Ideally, there should be a way to mark PropA with a custom attribute – say, [DependsOn(“PropB”)].

It turns out we can extend the MVVM Light “ObservableObject” class to do just that.

Continue reading

MSDN Locator Plugin for Windows Live Writer

One of the kindest things you can do in a blog post (or, perhaps, a StackOverflow answer) when referring to an MSDN-documented class, method, namespace, or property, is to provide a direct link to it.  Give the reader easy access to the relevant official documentation.

For blog posts, that’s even easier if you use this MSDN Locator plugin for Windows Live Writer.

Continue reading

Organizing a WPF Theme Into Files

I’ve been working on a next-generation version of one of our legacy products at work.  And lately I’ve been trying to give it a bit of modern flare, in the form of a customized WPF theme.  I’ve quickly found, however, that creating styles and templates makes for a GIGANTIC “Style.xaml” file.  As such, I’ve come to the conclusion that it’s best to divide your styles and resources into categories, creating a separate file for each category to keep them manageable.

Here are the categories I chose when dividing my theme into manageable pieces, as well as my thought process behind them.

Continue reading

Play Video Games on a Balance Ball

Sitting still is one of the worst things you can do.  Even if you work out regularly and eat healthy, sitting still for extended periods of time can be bad for your back, kill your metabolism, and, uh… kill you.  At least according to the Los Angeles Times.  According to their report, a single hour of TV-watching can result in “an 18% greater risk of dying from cardiovascular disease, an 11% greater risk of all causes of death, and a 9% increased risk of death from cancer.”

This is terrible news for people who work in an office and – more importantly – people who play video games.  I play video games!  (Actually, I work in an office, too, but that’s beside the point).  When you think about it, this “news” makes a lot of sense; who hasn’t felt like total crap after a long video game session?  Maybe your arm starts to fall asleep from poor circulation (or from laying on it too long).  Maybe your muscles cramp up.  Maybe you just have a general feeling of “crap, that was really unhealthy” when you finally shut down your system.

As it turns out, you can make some small steps to make this into a net positive experience.

What can be done to combat sedentary behavior while watching TV or playing video games?  Well, might I suggest a fitness ball; these inflatable wonders are actually quite forgiving on your back, and work your core muscles without you even realizing it; every small shift in your weight engages muscles in your abs and sides.  Sitting on a fitness ball – as opposed to a recliner or an overstuffed couch – forces you to keep a better posture, which helps blood flow.  You’re held – quite literally – on the edge of your seat while playing.

And they’re damn comfortable, too!

If you start to get a little restless from sitting on the fitness ball too long, you can just swivel your hips for a few minutes while playing.  Boom – instant core workout.  You can also get a decent core workout by simply leaning back a little – your ab muscles will engage to hold you upright.  Or, if you are an animated gamer who makes lots of exaggerated, energetic motions while playing, you will constantly be engaging your trunk without even realizing it.

Of course, this can be applied to your office space as well; but you may have to cut your desk down to size, so you can reach the keyboard.

Actually Get Useful Information From Expert’s Exchange!

It happens to all of us; you run into a problem with your programming, or some kind of system administration roadblock, and you turn to Google to solve all your problems.  The top result in your search looks exactly like the answer you were looking for, and you click it.


Expert’s | Exchange is a real sonuvabitch.  They generally have very high Google Pageranks, so they appear at the top of your search results, but then when you click the link, it tells you to sign up for a paid membership to see the “expert’s answers.”

As it turns out, you can access all that Expert’s Exchange goodness without paying a single cent.  And it requires NO hacking.  Ready to find out how?  Are you really ready?  You sure? Alright, here it is.

…Scroll down.

No, seriously, that’s it; just scroll further down the Expert’s Exchange page.  Expert’s Exchange wouldn’t have such a high Google Page-Rank if Google wasn’t able to crawl their pages, reading the content.  As a result, if you are linked to an Expert’s Exchange page via a Google search, all the supposedly “blocked” answers are revealed at the bottom of the page.  All that stuff at the top of the page asking you to sign up for a subscription?  Bologna.  Just scroll down, past the fake blocked-off answers, past their advertisements, past the “apparent” end of the page, and you’ll see all the answers that Google saw.  No hacking required; just a flick of your scroll wheel.

You’re welcome, world.

Note that if you are linked directly to the same page from some other website, the answers don’t appear.  You must be linked from your original Google search.

Don’t Freak Out If Your American Express Card Is Declined

I went to a great bar the other night to catch up with a friend of mine.  At the end of the night, I handed the waitress my American Express card – I try to use it as much as possible, since I get a decent amount of cash-back with it.

A few minutes later, she returns and curtly tells me that my card had been declined.

Surprised and confused, I settled my tab with a different card.  I had American Express on the phone before I had finished walking out the door.  Why was my card declined?  Did somebody get my account number?  I pay off my balance at the end of every month!

Whats that?  Oh.  Of course; the bar just doesn’t take American Express.

The moral of the story?  If your credit card is declined, make sure the establishment actually accepts your credit card, before you assume the worst.

Using DllImport To Import Core Methods Within Windows CE

When you design .NET applications, you may occasionally find yourself in need of core methods usually found in kernel32.dll, user32.dll, or ntdll.dll. These methods could include things like [cci]GetTickCount()[/cci] or [cci]MakeCriticalSectionGlobal()[/cci]. Normally, you would access them by simply declaring the method, preceded with a call to [cci]DllImport()[/cci] and the name of the core windows dll in which the method resides.  In Windows, this works just fine. Unfortunately, in Windows CE, these libraries do not exist.  If you attempt to import any methods from these libraries, your program will compile successfully, but you will be presented with a runtime error similar to this:

[cci]”Can’t find pinvoke dll ‘kernel32.dll'”[/cci]

To correct this problem, simply change your DllImport calls to import from “coredll.dll.”  On Windows CE, core methods were moved from kernel32.dll, user32.dll and ntdll.dll into this single library.