title: Get fields and values from an object by reflection in C#
body: <p>With the class below, I try to get :</p>
  
  <ul>
  <li>field name</li>
  <li>value</li>
  </ul>
  
  <p>I tried this piece of code :</p>
  
  <pre><code>Dictionary&lt;string, string&gt; listField = 
          membership.GetType()
              .GetFields(BindingFlags.NonPublic)
              .ToDictionary(f =&gt; f.Name,
                            f =&gt; (string)f.GetValue(null));
  </code></pre>
  
  <p>but I have nothing in the dictionary.</p>
  
  <p>Any idea ?                        </p>
  
  <pre><code>  [System.Diagnostics.DebuggerStepThroughAttribute()]
  [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
  [System.Runtime.Serialization.DataContractAttribute(Name="MyClass", Namespace="http://model.common.party.ent.gfdi.be")]
  [System.SerializableAttribute()]
  public partial class MyClass : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {
  
      [System.NonSerializedAttribute()]
      private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
  
      [System.Runtime.Serialization.OptionalFieldAttribute()]
      private string firstName;
  
      [System.Runtime.Serialization.OptionalFieldAttribute()]
      private string lastName;
  
      [System.Runtime.Serialization.OptionalFieldAttribute()]
      private System.Nullable&lt;long&gt; fieldA;   
  
      [System.Runtime.Serialization.OptionalFieldAttribute()]
      private bool fieldB;    
  
      [System.Runtime.Serialization.OptionalFieldAttribute()]
      private int fieldC;     
  }
  </code></pre>