title: Converting C++ pointer to member to C#
body: <p>I've been researching how to successfully convert C++ pointer-to-member to C# but I haven't found anything useful yet. Let's say I have this function.</p>
  
  <pre><code>typedef int STRUCT::*DEFINED;
  
  protected static Method(STRUCT* sampleStruct, DEFINED pMember)
      {
          return (sampleStruct-&gt;*pMember);
      }
  </code></pre>
  
  <p>I have learned through my research that ->* is a pointer-to-member. In this case, we send a pointer of a member variable in a struct called STRUCT. Since Method isn't really sure which member was sent as a parameter, it accesses it through pointer-to-member sampleStruct->*pMember.</p>
  
  <p>I think Reflection could help to convert this code to C#, or maybe Delegates, but I really don't know how to implement it, and I haven't found any similar example online. Any help will be appreciated. </p>
  
  <p>Thanks, 
  YT</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>This is how I have implemented this in C#.</p>
  
  <p>Instead of a struct, I created an enum, and a class to represent the C++ struct, like this:</p>
  
  <p>C++ struct</p>
  
  <pre><code>public struct ServerStats
  {
      int serverStat1;
      int serverStat2;
      int serverStat3;
      int serverStat4;
      int serverStat5;
  }
  </code></pre>
  
  <p>Now, in C#:</p>
  
  <pre><code>public enum ServerStatsEnum
  {
      serverStat1,
      serverStat2,
      serverStat3,
      serverStat4,
      serverStat5,
  }
  
  public class ServerStats
  {
      public int[] serverStatsArray;      
  
      public ServerStats()
      {
          int numElementsInEnum = Enum.GetNames(typeof(ServerStatsEnum)).Length;
          serverStatsArray = new int[numElementsInEnum];
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Now, I can access the elements of the array by calling the specific enum, like this:</p>
  
  <pre><code>public static void Operation(ServerStats server1, ServerStats server2, ServerStatsEnum index)
      {
          Console.WriteLine("serverStatsArray[{0}] in server1 is {1}", index, server1.serverStatsArray[(int)index]);
          Console.WriteLine("serverStatsArray[{0}] in server2 is {1}", index, server2.serverStatsArray[(int)index]);          
      }
  </code></pre>
  
  <p>It's more code, but it works natively in C# and it's more efficient than other solutions.</p>