title: How do I check if a class file has been changed before serializing it?
body: <p>We have a custom serialization process for a large number of C# types. However, regenerating all serialization information for all classes/types is time consuming and we were planning on optimizing the serialization process by computing the hash of the file and if different, we generate the serialized output, else we skip it. <strong>EDIT:</strong> We can store the hashes in a Dictionary which could be output to a file and re-read when processing. That's the current idea.</p>
  
  <p>Our current serialization processor works as follows - we add the types to be serialized to a repo:</p>
  
  <pre><code>SerializerRepo.Add(typeof(MyType)); //Add type to be serialized to a repo
  </code></pre>
  
  <p>And then (possibly elsewhere in code) have the serializer process the repo and output the custom XMLs etc.,</p>
  
  <pre><code>Serializer.WriteXML(SerializerRepo.GetTypes());
  </code></pre>
  
  <p><code>WriteXML</code> goes through each type and spews out an XML file for each type at a particular location. I need to optimize the <code>WriteXML</code> method to only serialize the class/type if it has changed, else let it be. </p>
  
  <p>This may not be the best way to do it and is open for refactoring suggestions. However, the current problem is how to ascertain if the class definition (or file) housing the class/type has changed in order to determine if the XML should be generated or not? </p>
  
  <p>Since there is no inherent relation between the type and the corresponding class since a class can be partial, .Net doesn't have any such mapping from types to class file and vice versa. However, we don't have any partial classes. But in our case, we seem to need the two (albeit unrelated) pieces of information - the file housing the type/class and the type itself. </p>
  
  <p>Two (possibly sub-optimal) ideas so far:</p>
  
  <ol>
  <li><p>Either we have the user specify the file name along with the type. But that'd not be amenable to any kind of refactoring where the file name is changed.</p></li>
  <li><p>Another solution is to manually read each .cs file and parse for <code>public class &lt;classname&gt;</code> and map it to every type. That seems like a huge overhead and not sure if it's a reliable way to do it.</p></li>
  </ol>
  
  <p>These are the only two ideas that I have but nothing concrete. Suggestions?</p>