title: Dynamic Type Conversion
body: <p>I have the following method</p>
  
  <pre><code>public static void SerializeToXMLFile(Object obj,Type type, string fileName)
  {
      XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
  
      ns.Add("", "");
  
      XmlSerializer serializer = new XmlSerializer(type);
      TextWriter tw = new StreamWriter(fileName);
  
      serializer.Serialize(tw, obj, ns);
      tw.Close();
  }
  </code></pre>
  
  <p>The problem is that notice in the line of code that obj will be serialized as an object.</p>
  
  <p><strong>serializer.Serialize(tw, obj, ns);</strong></p>
  
  <p>What I would prefer is that it is serlized as its relevant type for example:</p>
  
  <p><strong>serializer.Serialize(tw, (type) obj, ns);</strong></p>
  
  <p>How is this done? To get the type conversion to work from a dynamic variable?</p>