Convert some dynamic casts to static casts

There is no null check after these casts so if they fail they would
generate a NullReferenceException. By changing them to static casts
we would get InvalidCastException which are much less misleading.