Casting for people who read left-to-right
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
CastAs.Tests
CastAs
nuget
packages
.gitignore
CastAs.sln
readme.markdown

readme.markdown

CastAs

Here's code that I hate:

var viewName = ((ViewResult)actionResult).ViewName;

I have two problems with this code:

  1. The code doesn't read left-to-right. I have to start in the middle, look left to see what it's being cast as, and then further right to access the cast.
  2. Awkward parentheses.

This library contains one thing: a tiny static extension method on Object that lets me do this:

var viewName = actionResult.CastAs<ViewResult>().ViewName;
// or
var viewName = actionResult.CastTo<ViewResult>().ViewName;

CastAs will do a (obj as T) cast, and CastTo will do a (T)obj.

It's very minor, I know. But it helps me, especially when testing.

I also do a lot of MVC work, and this lets me test like this:

actionResult.CastAs<ViewResult>()
   .Model.CastAs<MyViewModel>()
   .LastName.ShouldEqual("Galt");