In my program, I will get a var
object at run time and I would like to write it to a binary file, but I couldn't write var
variable by using BinaryWriter
. It gives a compile error that cannot convert from 'object' to 'bool'
. How to solve it?
BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)
var obj = Convert.ChangeType(property.GetValue(objectToWrite, null), property.PropertyType);
writer.Write(obj); //Compile error