When I need this behavior, I look at how the framework does it. It's unfortunate that TypeNameHelper isn't public, but at least the source code is :)
TypeNameHelper