Sorry, I didn't read your post carefully. I thought type casting error is a compile time issue.
Can you provide a demo so we can test it? Runtime error is harder to fix, it will be easier if we can have the runable source code to test. If you're not willing to publicize the source code you can write a simplified demo that showing the problem.
Create a new folder, copy and paste all the necessary files except: the binary (exe file), *.bak, lib and backup folders. Compress the folder and send the zip here.