title: Get with Reflection fields that are not generated by the compiler
body: <p>Recently I was writing a method to construct a graph with the dependencies between classes using Reflection and found the following problem. My method analyzes the return type of property, generic arguments of class definition and instance fields of those classes.</p>
  
  <p>For inspection of instance fields of class I use the following method.</p>
  
  <pre><code>public static IEnumerable&lt;FieldInfo&gt; GetFields(Type classType)
  {
      return classType
          .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  }  
  </code></pre>
  
  <p>To test it, I write the following class definition:</p>
  
  <pre><code>static void Main(string[] args)
  {
      foreach (var fieldInfo in GetFields(typeof(A)))
          Console.WriteLine(fieldInfo.Name);
  
      Console.ReadKey();
  }
  
  class A
  {
      private ulong? _field1;
  
      public byte PropertyA { get; set; }
  
      public int PropertyB { get; set; }
  
      public bool PropertyC { get; set; }
  }
  </code></pre>
  
  <p>I was in shock for a few seconds, to see the result. It was when I remembered that  .NET generates an instance field, Set and Get methods to emulate the properties.</p>
  
  <p><img src="https://i.stack.imgur.com/0TiKF.png" alt="enter image description here"></p>
  
  <p>When I inspected with <a href="https://visualstudiogallery.msdn.microsoft.com/95789cdb-08f9-4dae-9b2f-fc45a452ad77?SRC=VSIDE" rel="noreferrer">.NET Reflector</a> the library to see the code generated by the compiler, I find the following definition.</p>
  
  <pre><code>class A
  {
      private ulong? _field1; 
  
      [CompilerGenerated]
      private Byte &lt;PropertyA&gt;k__BackingField;
  
      [CompilerGenerated]
      private Int32 &lt;PropertyB&gt;k__BackingField;
  
      [CompilerGenerated]
      private bool &lt;PropertyC&gt;k__BackingField;
  }
  </code></pre>
  
  <p>So I modified the method to exclude the fields with CompilerGenerated attribute and his name match with some property.</p>
  
  <pre><code>public static IEnumerable&lt;FieldInfo&gt; GetFields(Type classType)
  {
      var regex = new Regex(@"^&lt;(?&lt;PropertyName&gt;\w+)&gt;\w+$");
  
      var fieldInfoes = classType
              .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  
      foreach (var fieldInfo in fieldInfoes)
      {
          if (fieldInfo.GetCustomAttribute&lt;CompilerGeneratedAttribute&gt;() == null)
              yield return fieldInfo;
          else
          {
              var match = regex.Match(fieldInfo.Name);
              if (!match.Success) 
                  continue;
  
              var propertyName = match.Groups[@"PropertyName"].Value;
              if (classType.GetProperty(propertyName) == null)
                  yield return fieldInfo;
          }
      }
  }
  </code></pre>
  
  <p><strong>QUESTIONS</strong></p>
  
  <ul>
  <li>There is some combination of BindingFlags I'm missing for these fields?</li>
  <li>There is another way of getting these fields because this code appears to me that is like killing a mosquito with a bazooka?</li>
  </ul>
  
  <p>You can download the complete code <a href="https://gist.github.com/josephrodriguez/0e6da9ff2e0896fe15ce" rel="noreferrer">here</a>.</p>