Move dynamic_cast into assert, it's only for debug

and not checked in release.