Let's say I have an object of class MyClass with an object of SerialPort within it. I am trying to serialize and save to file my object of "MyClass", but I can't , because objects of class SerialPort are not serializable. Skipping to the chase, I have something like this:
[Serializable]
public class MyClass
{
public MyClass()
{
}
public SerialPort mySerialPort;
}
It appears C# does not allow me to unset or change type of mySerialPort. What would be the workaround in this situation?
mySerialPort = null; // obviously doesn't work
I need to access serial port from within the object of type MyClass, but I don't really need it when I serialize it. Is there a way to pass it as an argument during object construct so it's not a part of the object?
You can mark the field with [NonSerialized]
to exclude it from serialization.