Programmatically check for a change in a class in C#
<p>Is there a way to check for the size of a class in C#?</p>

<p>My reason for asking is:</p>

<p>I have a routine that stores a class's data in a file, and a different routine that loads this object (class) from that same file. Each attribute is stored in a specific order, and if you change this class you have to be reminded of these export/import routines needs changing.</p>

<p>An example in C++ (no matter how clumsy or bad programming this might be) would be
the following:</p>

<pre><code>#define PERSON_CLASS_SIZE 8
class Person
{
  char *firstName;
}
</code></pre>

<p>...</p>

<pre><code>bool ExportPerson(Person p)
{
  if (sizeof(Person) != PERSON_CLASS_SIZE ) 
  {  
     CatastrophicAlert("You have changed the Person class and not fixed this export routine!")
  }
}
</code></pre>

<p></p>

<p>Thus before compiletime you need to know the size of Person, and modify export/import routines with this size accordingly.</p>

<p>Is there a way to do something similar to this in C#, or are there other ways of "making sure" a different developer changes import/export routines if he changes a class.</p>

<p>... Apart from the obvious "just comment this in the class, this guarantees that a developer never screws things up"-answer.</p>

<p>Thanks in advance.</p>
