Fixed Invalid cast expection in Writer