Replace dangerous static cast by dynamic cast + assert on result.