1

I have a few classes that I have to refactor and my particular concern is that they are marked as Serializable. I know with 100% certainty that these classes are parsed out into a file and read back in later.

I am wondering what are some things I should be making sure I consider as I make changes?


Properties

If I remember correctly, when using the Serializable attribute on a class, only the fields are parsed (I can't remember if access level matters). But what does this mean for auto-generated properties?

From my understanding the compiler creates an underlying field for you to ease the development process so wouldn't this mean that in modern code properties are in fact being parsed?

Also, if my memory serves correct, the fields behind auto-generated properties get some pretty weird names. Do these change with each build, or do they remain consistent unless you change the name of the property?


Combining Fields

A few of these classes have fields which could be combined into more complex types. For example:

public float mouseX;
public float mouseY;
public float positionX;
public float positionY;
public float positionZ;

These can be simplified to VectorN types such as:

public Vector2 mousePosition;
public Vector3 position;

Since this is a really big difference between old and new versions, and it is way more than just a name change; what are some things I should really consider prior to making this change?


Moving Fields to new Classes

I do have a particular case where a class contains multiple functionalities that can be broken out into individual classes for better readability, reusability, and maintainability. If this step is taken, it is definitely a big change from the old version and I'm sure it can cause even bigger problems. What are things to consider with this type of change?


Note

There are several classes that implement ISerializable which should make everything a little easier to transition.


All in all, what are some other things that most do not think about on the first run until it all blows up?

closed as too broad by gnat, Thomas Owens Jan 23 at 16:25

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    I'm assuming that you want to be able to deserialise "old" data into the "new" class, because if you don't care then there's no problem? – Caleth Jan 23 at 15:08
  • @Caleth that is correct! – PerpetualJ Jan 23 at 15:09
  • 1
    The C# serialization documentation says the versioning of objects may create problems if you dont use custom serialization by inheriting from ISerializable (docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…). So it is not guaranteed that serialization still works after refactoring. – pschill Jan 23 at 15:23
  • I guess you could get your question reopened by splitting it into 3 different questions. – Doc Brown Jan 23 at 16:35